Maven利用Profile构建不同环境的部署包

项目开发好以后,通常要在多个环境部署,像我曾经呆过的公司最多达5种环境:本机环境(local)、(开发小组内自测的)开发环境(dev)、(提供给测试团队的)测试环境(sit)、预发布环境(pre)、正式生产环境(prod),每种环境都有各自的配置参数,比如:数据库连接、远程调用的ws地址等等。如果每个环境build前手动修改这些参数,显然太不fashion.

maven早就考虑到了这些问题,看下面的pom片段:

  1. <profiles>  
  2.     <profile>  
  3.         <!-- 本地环境 -->  
  4.         <id>local</id>  
  5.         <properties>  
  6.             <jdbc.url>jdbc:oracle:thin:@localhost:1521:orcl</jdbc.url>  
  7.             <jdbc.username>***</jdbc.username>  
  8.             <jdbc.password>***</jdbc.password>  
  9.         </properties>  
  10.     </profile>  
  11.     <profile>  
  12.         <!-- 开发环境 -->  
  13.         <id>dev</id>  
  14.         <properties>  
  15.             <jdbc.url>jdbc:oracle:thin:@172.21.129.51:1521:orcl</jdbc.url>  
  16.             <jdbc.username>***</jdbc.username>  
  17.             <jdbc.password>***</jdbc.password>  
  18.         </properties>  
  19.         <!-- 默认激活本环境 -->  
  20.         <activation>  
  21.             <activeByDefault>true</activeByDefault>  
  22.         </activation>  
  23.     </profile>  
  24. </profiles>  

profiles节点中,定义了二种环境:local、dev(默认激活dev环境),可以在各自的环境中添加需要的property值,接下来修改build节点,参考下面的示例:

  1. <build>  
  2.     <resources>  
  3.         <resource>  
  4.             <directory>src/main/resources</directory>  
  5.             <filtering>true</filtering>  
  6.         </resource>  
  7.     </resources>  
  8.     <plugins>  
  9.         <plugin>  
  10.             <groupId>org.apache.maven.plugins</groupId>  
  11.             <artifactId>maven-compiler-plugin</artifactId>  
  12.             <version>2.3</version>  
  13.             <configuration>  
  14.                 <source>1.7</source>  
  15.                 <target>1.7</target>  
  16.                 <encoding>utf-8</encoding>  
  17.             </configuration>  
  18.         </plugin>  
  19.     </plugins>  
  20. </build>  

resource节点是关键,它表明了哪个目录下的配置文件(不管是xml配置文件,还是properties属性文件),需要根据profile环境来替换属性值。

通常配置文件放在resources目录下,build时该目录下的文件都自动会copy到class目录下

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:jee="http://www.springframework.org/schema/jee"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd>  
  8.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  9.             <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  10.             <property name="url" value="${jdbc.url}" />  
  11.             <property name="username" value="${jdbc.username}" />  
  12.             <property name="password" value="${jdbc.password}" />  
  13.         </bean>  
  14.   
  15. </beans>  

各属性节点的值,用占位符"${属性名}"占位,maven在package时,会根据profile的环境自动替换这些占位符为实际属性值。

默认情况下: 

maven package

将采用默认激活的profile环境来打包,也可以手动指定环境,比如:

maven package -P dev

将自动打包成dev环境的部署包(注:参数P为大写)


这时候很多人会有一个疑问,生产环境,通常是在webserver(比如weblogic上),它配置的一个JNDI数据源,你这显然不适用啊,客官别急,我将会给两种方案用来解决问题。

第一种,简单暴力的

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jee="http://www.springframework.org/schema/jee"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">  
  8.   
  9.         <bean id="${db-source-jdbc}" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  10.             <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  11.             <property name="url" value="${jdbc.url}" />  
  12.             <property name="username" value="${jdbc.username}" />  
  13.             <property name="password" value="${jdbc.password}" />  
  14.   
  15.         </bean>  
  16.         <jee:jndi-lookup id="${db-source-jndi}" jndi-name="${jndi.name}" />  
  17. </beans>  

POM文件

  1. <profiles>  
  2.     <profile>  
  3.         <!-- 本地环境 -->  
  4.         <id>local</id>  
  5.         <properties>  
  6.             <db-source-jdbc>dataSource</db-source-jdbc>  
  7.             <db-source-jndi>NONE</db-source-jndi>  
  8.             <db-url>jdbc:oracle:thin:@localhost:1521:XE</db-url>  
  9.             <db-username>***</db-username>  
  10.             <db-password>***</db-password>  
  11.         </properties>  
  12.         <!-- 默认激活本环境 -->  
  13.         <activation>  
  14.             <activeByDefault>true</activeByDefault>  
  15.         </activation>  
  16.     </profile>  
  17.     <profile>  
  18.         <!-- 开发环境 -->  
  19.         <id>pro</id>  
  20.         <properties>  
  21.             <db-source-jdbc>NONE</db-source-jdbc>  
  22.             <db-source-jndi>dataSource</db-source-jndi>  
  23.             <jndi.name>somnus</jndi.name>  
  24.         </properties>  
  25.     </profile>  
  26. </profiles>  


mvn clean package -P local打包本地开发环境时,将生成

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jee="http://www.springframework.org/schema/jee"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">  
  8.   
  9.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  10.             <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  11.             <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" />  
  12.             <property name="username" value="***" />  
  13.             <property name="password" value="***" />  
  14.   
  15.         </bean>  
  16.         <jee:jndi-lookup id="NONE" jndi-name="${jndi.name}" />  
  17. </beans>  

mvn clean package -P pro打包本地开发环境时,将生成

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jee="http://www.springframework.org/schema/jee"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">  
  8.   
  9.         <bean id="NONE" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
  10.             <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  11.             <property name="url" value="${jdbc.url}" />  
  12.             <property name="username" value="${jdbc.username}" />  
  13.             <property name="password" value="${jdbc.password}" />  
  14.   
  15.         </bean>  
  16.         <jee:jndi-lookup id="dataSource" jndi-name="somnus" />  
  17. </beans>  

当bean的id为NONE意味着什么就不要我多说了吧


第二种,高级优美的

我们将利用Spring的profile配置,把我们需要的环境全部配置好,最终由web.xml中的spring.profiles.default决定调用哪个环境配置

先给出bean的配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:jee="http://www.springframework.org/schema/jee"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd    
  9.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">  
  10.   
  11.     <!-- ************************************************************************************************* -->  
  12.     <!-- * 环境配置切换(在web.xml中的spring.profiles.default上启用) * -->  
  13.     <!-- ************************************************************************************************* -->  
  14.     <!-- 开发环境 -->  
  15.     <beans profile="dev">  
  16.         <context:property-placeholder  
  17.             ignore-unresolvable="true" location="classpath:/env/configurations-dev.properties" />  
  18.         <!-- 数据源配置-DBCP数据库连接池 -->  
  19.         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  20.             destroy-method="close">  
  21.             <property name="driverClassName" value="${jdbc.driverClassName}" />  
  22.             <property name="url" value="${jdbc.url}" />  
  23.             <property name="username" value="${jdbc.username}" />  
  24.             <property name="password" value="${jdbc.password}" />  
  25.             <property name="initialSize" value="${jdbc.initialSize}" />  
  26.             <property name="maxActive" value="${jdbc.maxActive}" />  
  27.             <property name="maxIdle" value="${jdbc.maxIdle}" />  
  28.             <property name="minIdle" value="${jdbc.minIdle}" />  
  29.             <property name="maxWait" value="${jdbc.maxWait}" />  
  30.             <property name="defaultAutoCommit" value="false" />  
  31.         </bean>  
  32.     </beans>  
  33.   
  34.     <!-- SIT环境 -->  
  35.     <beans profile="sit">  
  36.         <context:property-placeholder  
  37.             ignore-unresolvable="true" location="classpath:/env/configurations-sit.properties" />  
  38.         <!-- 数据源配置,使用应用服务器的数据库连接池 -->  
  39.         <jee:jndi-lookup id="dataSource" jndi-name="${jndi.name}" />  
  40.     </beans>  
  41.   
  42.     <!-- PRD环境 -->  
  43.     <beans profile="prd">  
  44.         <context:property-placeholder  
  45.             ignore-unresolvable="true" location="classpath:/env/configurations-prd.properties" />  
  46.         <!-- 数据源配置,使用应用服务器的数据库连接池 -->  
  47.         <jee:jndi-lookup id="dataSource" jndi-name="${jndi.name}" />  
  48.     </beans>  
  49. </beans>  

现在是至关重要的web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  4.     <!-- spring配置文件位置 -->  
  5.     <context-param>  
  6.         <param-name>contextConfigLocation</param-name>  
  7.         <param-value>classpath:/spring-basic.xml</param-value>  
  8.     </context-param>  
  9.     <context-param>  
  10.                <param-name>spring.profiles.default</param-name>  
  11.                <param-value>${profiles.active}</param-value>  
  12.        </context-param>  
  13.   
  14.     <!-- spring监听器 -->  
  15.     <listener>  
  16.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.     </listener>  
  18. </web-app>  

POM文件配置,主要看maven-war-plugin插件的配置
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <build>  
  5.         <plugins>  
  6.             <!-- 打war包插件  -->  
  7.             <plugin>  
  8.                 <artifactId>maven-war-plugin</artifactId>  
  9.                 <configuration>  
  10.                     <packagingExcludes>  
  11.                         WEB-INF/classes/logback-test.xml  
  12.                     </packagingExcludes>  
  13.                     <webResources>  
  14.                         <resource>  
  15.                             <!-- 是否过滤文件,也就是是否启动auto-config的功能 -->  
  16.                             <filtering>true</filtering>  
  17.                             <!-- 元配置文件的目录,相对于pom.xml文件的路径 -->  
  18.                             <directory>${basedir}/src/main/webapp/WEB-INF</directory>  
  19.                             <targetPath>WEB-INF</targetPath>  
  20.                             <!-- 包括以下文件夹下的文件 -->  
  21.                             <includes>  
  22.                                 <exclude>**/web.xml</exclude>  
  23.                             </includes>  
  24.                         </resource>    
  25.                     </webResources>  
  26.                     <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>   
  27.                     <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>  
  28.                 </configuration>  
  29.             </plugin>  
  30.         </plugins>  
  31.     </build>  
  32.     <profiles>  
  33.         <profile>  
  34.             <id>dev</id>  
  35.             <activation>  
  36.                 <activeByDefault>true</activeByDefault>  
  37.             </activation>  
  38.             <properties>  
  39.                 <profiles.active>dev</profiles.active>  
  40.             </properties>  
  41.         </profile>  
  42.         <profile>  
  43.             <id>sit</id>  
  44.             <properties>  
  45.                 <profiles.active>sit</profiles.active>  
  46.             </properties>  
  47.         </profile>  
  48.         <profile>  
  49.             <id>prd</id>  
  50.             <properties>  
  51.                 <profiles.active>prd</profiles.active>  
  52.             </properties>  
  53.         </profile>  
  54.     </profiles>  
  55. </project>  



package打包运行完,你将看到


圈红的地方将不再是占位符,那么我们启动的时候web项目将会根据此配置直接去激活sit的配置,现在是不是觉得很简单了呢。


然而还没有完,如果只是到此,你只是能打包(mvn package),才能用上这么方便的东西,那如果我想在本地tomcat开发环境直接运行呢,

不管是采用tomcat直接启动,还是 mvn tomcat7:run 的方式,你会发现占位符反而会坑了项目,

由于内置tomcat是直接编译源码然后放到指定的位置去加载 ,所以上述的方法对于在内置tomcat运行是行不通的。

那就要另觅他法了,下面介绍一种探索出的一个方法: 
首先在main目录下新建一个profile文件夹,将WEB-INF下面的web.xml复制过来一份 
然后将WEB-INF下面的web.xml中的占位符修改成默认的配置(即没有占位符的,在本地测试用的profile值,如dev) 
profile文件夹下用保留占位符的web.xml配置 

然后对上述所描述的pom文件再做进一步修改,如下

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <build>  
  5.         <plugins>  
  6.             <!-- 打war包插件  -->  
  7.             <plugin>  
  8.                 <artifactId>maven-war-plugin</artifactId>  
  9.                 <configuration>  
  10.                     <packagingExcludes>  
  11.                         WEB-INF/classes/logback-test.xml  
  12.                     </packagingExcludes>  
  13.                     <webResources>  
  14.                         <resource>  
  15.                             <!-- 是否过滤文件,也就是是否启动auto-config的功能 -->  
  16.                             <filtering>true</filtering>  
  17.                             <!-- 元配置文件的目录,相对于pom.xml文件的路径 -->  
  18.                             <directory>${basedir}/src/main/profile</directory>  
  19.                             <targetPath>WEB-INF</targetPath>  
  20.                             <!-- 包括以下文件夹下的文件 -->  
  21.                             <includes>  
  22.                                 <exclude>**/web.xml</exclude>  
  23.                             </includes>  
  24.                         </resource>    
  25.                     </webResources>  
  26.                     <warSourceDirectory>${basedir}/src/main/webapp</warSourceDirectory>   
  27.                     <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>  
  28.                 </configuration>  
  29.             </plugin>  
  30.         </plugins>  
  31.     </build>  
  32.     <profiles>  
  33.         <profile>  
  34.             <id>dev</id>  
  35.             <activation>  
  36.                 <activeByDefault>true</activeByDefault>  
  37.             </activation>  
  38.             <properties>  
  39.                 <profiles.active>dev</profiles.active>  
  40.             </properties>  
  41.         </profile>  
  42.         <profile>  
  43.             <id>sit</id>  
  44.             <properties>  
  45.                 <profiles.active>sit</profiles.active>  
  46.             </properties>  
  47.         </profile>  
  48.         <profile>  
  49.             <id>prd</id>  
  50.             <properties>  
  51.                 <profiles.active>prd</profiles.active>  
  52.             </properties>  
  53.         </profile>  
  54.     </profiles>  
  55. </project>  

最后见证奇迹 
执行命令 tomcat7:run ,完美解决,启动成功

后面我再补充一个小知识点,既然Spring给我们提供了,这么好的配置


web项目,靠web.xml可以激活任一数据源,那如果是单元测试,我将如何激活呢

这点Spring当然考虑到了,不过版本必须是Spring3.0以上 ,重要的事情说三遍,3.0   3.0   3.0啊 亲!!!

  1. /** 
  2.  * @author Somnus 
  3.  * @version 1.0 
  4.  * @Description 后台单元测试基类 
  5.  * @date 2012-11-19 
  6.  */  
  7. @RunWith(SpringJUnit4ClassRunner.class)  
  8. @ContextConfiguration(locations = "classpath:spring-basic.xml")  
  9. @ActiveProfiles("dev")  
  10. public abstract class AbstractTestSupport {  
  11.     protected transient Logger log = LoggerFactory.getLogger(this.getClass());  
  12.   
  13.     @Rule  
  14.     public TestName name = new TestName();  
  15.   
  16.     @Before  
  17.     public void setUp() {  
  18.         log.info("调用测试方法: {}"new Object[]{name.getMethodName()});  
  19.     }  
  20.   
  21.     @After  
  22.     public void tearDown() {  
  23.     }  
  24. }  

留心 @ActiveProfiles("dev") ,想用哪个数据源,就用哪个数据源
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值