工作中经常遇到开发、测试、生产等多个环境切换,profile可以解决,目前主流的是spring profile和maven profile两种。以我项目配置文件为例,结构如下,主要的改变是在properties里:
- 一、spring profile
1、在spring的配置文件中配置profile,下面是我的app-context-profile.xml,把profile的配置独立出来,然后引用该配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<description>spring profile配置</description>
<!-- 开发环境配置文件 -->
<beans profile="development">
<context:property-placeholder
location="classpath*:properties/development/*.properties" />
</beans>
<!-- 测试环境配置文件 -->
<beans profile="test">
<context:property-placeholder
location="classpath*:properties/test/*.properties" />
</beans>
<!-- 生产环境配置文件 -->
<beans profile="production">
<context:property-placeholder
location="classpath*:properties/production/*.properties" />
</beans>
</beans>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/app-context.xml,
classpath:spring/app-context-mybatis.xml,
classpath:spring/app-context-service.xml,
classpath:spring/app-context-profile.xml,
classpath:spring/app-context-other.xml
</param-value>
</context-param>
classpath:spring/app-context-profile.xml,
classpath:spring/app-context-other.xml
</param-value>
</context-param>
2、在web.xml中配置调用
<!-- 在上下文context-param中设置profile.default的默认值 -->
<context-param>
<param-name>spring.profiles.default</param-name>
<param-value>development</param-value>
</context-param>
<!-- 在上下文context-param中设置profile.active的默认值 -->
<!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
<!-- <param-value>的值和前面配置的beans profile一致,
需要调用哪种环境修改param-value即可 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>test</param-value>
</context-param>
test</param-value>
</context-param>
spring profile适合所有工程,但是切换环境仍然需要修改web.xml。下面的maven profile可以实现零文件修改
- 二、maven profile
1、pom.xml文件配置
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>development</id>
<properties>
<profiles.active>development</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
</properties>
</profile>
<profile>
<!-- 生产环境 -->
<id>production</id>
<properties>
<profiles.active>production</profiles.active>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 资源根目录排除各环境的配置,使用单独的资源目录来指定 -->
<excludes>
<exclude>properties/test/**</exclude>
<exclude>properties/production/**</exclude>
<exclude>properties/development/**</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/properties/${profiles.active}</directory>
<!-- 由于上方没有把profile的文件打包,在此处单独指定profile需要的
properties打包到指定位置,如果没有该配置profile需要的文件会采用maven
的默认配置打到resources目录下而不是resources/properties下 -->
<targetPath>properties</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<!-- 激活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>
</plugins>
<finalName>dataservice-gateway</finalName>
</build>
此配置已经可以采用profile打包了,运行mvn clean install -Ptest就可以打出test需要的war包,但这样还不能和eclipse tomcat中发布的一致。
2、与eclipse tomcat集成(非maven tomcat插件)
右键选中程序-properties-maven-录入需要运行的profile
CRTL+ALT+P重新把tomcat publish一下即可
如果maven打包后遇到“Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed”,maven-update projects一下