maven整合spring profile实现多环境自动切换
1、resources目录下添加不同环境的配置文件
2、applicationContext文件增加profile配置
<beans profile="dev">
<import resource="classpath:dev/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:dev/*.properties"/>
</beans>
<beans profile="test">
<import resource="classpath:test/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:test/*.properties"/>
</beans>
<beans profile="prod">
<import resource="classpath:prod/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:prod/*.properties"/>
</beans>
3、web.xml增加激活profile的配置
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>${profile.active}</param-value>
</context-param>
4、pom.xml文件增加profile配置
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>local</id>
<properties>
<profile.active>local</profile.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
5、pom.xml增加打包插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>ROOT</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>
6:打包命令
如:mvn clean package -DskipTests=true -P prod -P参数为指定的打包环境
实现原理参考:http://zilongsky-gmail-com.iteye.com/blog/2032001
1、resources目录下添加不同环境的配置文件
2、applicationContext文件增加profile配置
<beans profile="dev">
<import resource="classpath:dev/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:dev/*.properties"/>
</beans>
<beans profile="test">
<import resource="classpath:test/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:test/*.properties"/>
</beans>
<beans profile="prod">
<import resource="classpath:prod/remote-sevlet.xml"/>
<context:property-placeholder location="classpath:prod/*.properties"/>
</beans>
3、web.xml增加激活profile的配置
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>${profile.active}</param-value>
</context-param>
4、pom.xml文件增加profile配置
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>local</id>
<properties>
<profile.active>local</profile.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
5、pom.xml增加打包插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>ROOT</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>
6:打包命令
如:mvn clean package -DskipTests=true -P prod -P参数为指定的打包环境
实现原理参考:http://zilongsky-gmail-com.iteye.com/blog/2032001