Maven 整合 spring profile实现多环境自动切换
Maven 整合 spring profile实现多环境自动切换
profile主要用在项目多环境运行的情况下,比如开发环境、测试环境、线上生产环境。 <beans profile="xmj_old">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
<property name="password" value="123456" />
<property name="username" value="abc" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages_zh_CN</value>
<value>classpath:messages/messages_en_US-xmj_old</value>
</list>
</property>
</bean>
</beans>
<beans profile="xmj_new">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/db1?characterEncoding=utf8" />
<property name="password" value="123456" />
<property name="username" value="abc" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages_zh_CN</value>
<value>classpath:messages/messages_en_US-xmj_new</value>
</list>
</property>
</bean>
</beans>
...
...
...
这里的message_en_US-*系列属性文件是配置站点的名称和黑名单的位置: resource.blacklist.dir=/var/resource/blacklist.txt
system.title.id=system.xmj_old.title
激活spring的profile <context-param>
<param-name>spring.profiles.active</param-name>
<param-value>xmj_old</param-value>
</context-param>
这样就激活spring配置文件中的profile为xmj_old的配置,但这样手动配置依然要改web.xml配置。 <profiles>
<profile>
<id>xmj_old</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profiles.activation>xmj_old</profiles.activation>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 配置项目自动发布服务器 -->
<url>http://xx.xx.xx.xx:8080/manager/text</url>
<path>/xmj-manager</path>
<server>Tomcat</server>
<warFile>target/${profiles.activation}.war</warFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>xmj_new</id>
<properties>
<profiles.activation>xmj_new</profiles.activation>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<!-- 配置项目自动发布服务器 -->
<url>http://xx2.xx.xx.xx:8080/manager/text</url>
<path>/xmj-manager</path>
<server>Tomcat</server>
<warFile>target/${profiles.activation}.war</warFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
...
...
...
</profiles>
activeByDefault标签配置true表示默认激活的环境,直接install就是使用此配置的环境。 <context-param>
<param-name>spring.profiles.active</param-name>
<param-value>${profiles.activation}</param-value>
</context-param>
这还不算完,还要配置Maven的一个插件: <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
打包的时候Maven会去自动修改web.xml文件,根据占位符自动替换为对应的属性值 clean install
前面配置的默认环境是xmj_old这个,在执行完命令之后target文件夹下面出现了一个名为xmj_old.war的war包 clean install -P xmj_new
-P 参数后面加上profile的id即可完成自动切换,执行完之后看下是否已经自动切换到id为xmj_new的profile环境下 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<directory>src/main/webapp</directory>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
修改为: <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>${profiles.activation}</warName>
<!-- 激活spring profile -->
<webResources>
<resource>
<filtering>true</filtering>
<!-- 这里是刚刚创建的目录 -->
<directory>src/main/profile</directory>
<!-- 目标目录为WEB-INF -->
<targetPath>WEB-INF</targetPath>
<includes>
<include>**/web.xml</include>
</includes>
</resource>
</webResources>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
最后见证奇迹 install -P naruto
在target下面出现根据profile打的war包naruto.war eclipse:eclipse -Dwtpversion=1.5
编译成正常的eclipse工程,然后加入tomcat服务器,你会发现能运行成功。 |
Maven 整合 spring profile实现多环境自动切换的更多相关文章
- 使用maven整合spring+springmvc+mybatis
使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...
- springboot+maven整合spring security
springboot+maven整合spring security已经做了两次了,然而还是不太熟悉,这里针对后台简单记录一下需要做哪些事情,具体的步骤怎么操作网上都有,不再赘述.1.pom.xml中添 ...
- Maven整合Spring与Solr
首先,在maven的pom.xml文件中配置对spring和solrj客户端的依赖: <project xmlns="http://maven.apache.org/POM/4.0.0 ...
- Maven Filter与Profile隔离生产环境与开发环境
Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...
- (转载)maven profile多环境自动切换配置
原文:https://www.cnblogs.com/adeng/p/7059588.html 痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环 ...
- maven学习利用Profile构建不同环境的部署包
项目开发好以后,通常要在多个环境部署,象我们公司多达5种环境:本机环境(local).(开发小组内自测的)开发环境(dev).(提供给测试团队的)测试环境(test).预发布环境(pre).正式生产环 ...
- spring Profile 为不同环境提供不同的配置支持
说明 Profile为在不同环境下使用不同的配置提供了支持(开发环境下的配置和生产环境下的配置肯定是不同的, 例如, 数据库的配置) . 在spring开发中用@Profile 注解使用来选择行配置系 ...
- Maven + Spring 进行多环境自动切换功能
在pom.xml的<project></project>的最下放写入如下代码: <!-- profiles setting start [mvn install -P x ...
- maven profile多环境自动切换配置,配置分离,排除文件
痛点: 在java开发的过程中,我们经常要面对各种各样的环境,比如开发环境,测试环境,正式环境,而这些环境对项目的需求也不相同. 在此之前,我们往往需要手动去修改相对应的配置文件然后打成war,才能部 ...
随机推荐
- Excel实用技巧
情景:有时候,我们写了一个公式,然后想在其他行也套用这个公式,一般人都是把鼠标放在那个公式所在的单元格的右下角,然后往下拉,数据量少的时候还好,数据量大的时候就不太好操作了,此时,我们需要一个好方法. ...
- SHA1算法
public string SHA1_Hash(string str_sha1_in) { SHA1 sha1 = new SHA1CryptoServicePro ...
- CentOS6.5上安装MySQL5.6
1.准备数据存放的文件系统 新建一个逻辑卷,并将其挂载至特定目录即可.这里不再给出过程. 这里假设其逻辑卷的挂载目录为/data,而后需要创建/data/mysqldata目录做为mysql数据的存放 ...
- Bloom Filter学习
参考文献: Bloom Filters - the math http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html B ...
- 笔记19-徐 如何在超大型数据库上运行DBCC CHECKDB
) --以上方法TB级数据库的DBA可以考虑试试
- Reveal的使用及破解方法
Reveal的使用其实真的很简单,就如第一张镇楼图的效果一样.中间是3D可视化当前APP页面的视图,左侧则是这些UI元素和层次结构,而右侧则是View的属性,你可以修改View的颜色.frame等等, ...
- iOS(本地通知与远程通知)
iOS 推送通知有两种:本地推送.远程推送. 本地推送 : 在不需要联网的情况下,由APP发出推送,常用于某一时刻的通知,如闹钟.本地通送有局限性在于当APP处于后台或者退出时就无法发出通知. 远程 ...
- svn命令
1.检出.更新.提交 svn chectout http://svn_server/xxx_repository/trunk svn update svn commit -m "XXX&qu ...
- 使用UDP协议与韩国OACIS压机通讯
最近一个项目需要发送SN给OACIS, 研究了一下OACIS文档, 从文档中可以看出,传输协议只能使用UDP,切传输命令为>SN1; + SN + ; + LF LF的定义在Linux和Uni ...
- eclipse黑色主题
Lennon http://www.cnblogs.com/csulennon/p/4231405.html