使用maven的profile切换项目各环境的参数

http://blog.csdn.net/massivestars/article/details/53510586


 maven默认的运行环境是test,我打包的时候怎么样才能切换到生产环境 







在实际开发项目中,常常有几种环境,一般情况下最少有三种环境:开发测试正式。

各个环境之间的参数各不相同,比如MySQLRedis等不同环境的host不一样,若每个环境都手动替换环境很容易出错,这里我们利用maven的profile功能切换环境。


本文的项目结构图:


src/main/resources/dev  目录的properties是开发环境的配置项目
src/main/resources/test  目录的properties是测试环境的配置项目


在pom.xml定义环境的profile
[html]  view plain  copy
 print ?
  1. <profiles>  
  2.     <profile>  
  3.         <id>dev</id>  
  4.         <properties>  
  5.             <profiles.activation>dev</profiles.activation>  
  6.         </properties>  
  7.         <activation>  
  8.             <activeByDefault>true</activeByDefault>  
  9.         </activation>  
  10.     </profile>  
  11.     <profile>  
  12.         <id>test</id>  
  13.         <properties>  
  14.             <profiles.activation>test</profiles.activation>  
  15.         </properties>  
  16.     </profile>  
  17. </profiles>  

activeByDefault标签的值为true的话表示默认的profile,使用mvn install命令起作用的就是它,这里为dev


resources标签定义要包含的资源,在下面的配置下package阶段会把resources文件夹里的 ${profiles.activation}/* 文件打包
这里的${profiles.activation}由命令maven的-P选项指定,例:mvn install -Ptest 就是打包 test/* 即test目录下的所有文件 
[html]  view plain  copy
 print ?
  1. <resources>  
  2.     <resource>  
  3.         <directory>src/main/resources</directory>  
  4.         <!-- **/*.properties 是指包括根目录或子目录所有properties类型的文件 -->  
  5.         <includes>  
  6.             <include>**/*.properties</include>  
  7.             <include>**/*.xml</include>  
  8.         </includes>  
  9.   
  10.         <!-- 排除dev、test目录下的文件 -->  
  11.         <excludes>  
  12.             <exclude>dev/*</exclude>  
  13.             <exclude>test/*</exclude>  
  14.         </excludes>  
  15.     </resource>  
  16.     <resource>  
  17.         <directory>src/main/resources</directory>  
  18.         <!-- 包含,若没有指定则默认为 activeByDefault 标签定义的profile -->  
  19.         <includes>  
  20.             <include>${profiles.activation}/*</include>  
  21.         </includes>  
  22.     </resource>  
  23. </resources>  





applicationContext.xml文件如下
[html]  view plain  copy
 print ?
  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.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byType">  
  7.   
  8.     <!-- Annotation Config -->  
  9.     <context:annotation-config/>  
  10.   
  11.   
  12.     <!-- 取${profiles.activation:dev}表示取${profiles.activation}的值,若没有则指定dev -->  
  13.     <bean id="propertyConfig"  
  14.           class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  15.         <property name="locations">  
  16.             <list>  
  17.                 <value>classpath:${profiles.activation:dev}/jdbc.properties</value>  
  18.             </list>  
  19.         </property>  
  20.     </bean>  
  21.   
  22.     <!-- ==============配置数据源============== -->  
  23.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">  
  24.         <!-- 基本属性 url、user、password -->  
  25.         <property name="url" value="${jdbc.url}" />  
  26.         <property name="username" value="${jdbc.username}" />  
  27.         <property name="password" value="${jdbc.password}" />  
  28.   
  29.         <!-- 配置初始化大小、最小、最大 -->  
  30.         <property name="initialSize" value="1" />  
  31.         <property name="minIdle" value="1" />  
  32.         <property name="maxActive" value="20" />  
  33.   
  34.         <!-- 配置获取连接等待超时的时间 -->  
  35.         <property name="maxWait" value="60000" />  
  36.   
  37.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  38.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  39.   
  40.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  41.         <property name="minEvictableIdleTimeMillis" value="300000" />  
  42.   
  43.     </bean>  
  44.   
  45. </beans>  

这里取${profiles.activation:dev}是取${profiles.activation}的值,若不存在,则默认为dev;
若值为dev,locations的值为 classpath:dev/jdbc.properties。
${profiles.activation}是在web.xml里的context-param取值
[html]  view plain  copy
 print ?
  1. <context-param>  
  2.     <param-name>profiles.activation</param-name>  
  3.     <param-value>${profiles.activation}</param-value>  
  4. </context-param>  

值得注意的是,${profiles.activation}由于有默认值的存在,applicationContext.xml不需要启动web容器去读取web.xml中的${profiles.activation},
这有效的保障了使用JUnit进行单元测试,也就是说在maven的test目录里的测试用例可以正常运行。

现在的问题就成了如何把maven里激活的profile值传进来,使用 maven-war-plugin能在maven install的时期会设置web.xml占位符值${}的值 
[html]  view plain  copy
 print ?
  1. <plugin>  
  2.       <groupId>org.apache.maven.plugins</groupId>  
  3.           <artifactId>maven-war-plugin</artifactId>  
  4.       <configuration>  
  5.           <warName>${profiles.activation}</warName>  
  6.           <!-- 激活spring profile -->  
  7.           <webResources>  
  8.               <resource>  
  9.                   <filtering>true</filtering>  
  10.                   <directory>src/main/webapp</directory>  
  11.                   <includes>  
  12.                       <include>**/web.xml</include>  
  13.                   </includes>  
  14.               </resource>  
  15.           </webResources>  
  16.           <warSourceDirectory>src/main/webapp</warSourceDirectory>  
  17.           <webXml>src/main/webapp/WEB-INF/web.xml</webXml>  
  18.       </configuration>  
  19.   </plugin>  



所有文件配置完毕,使用  mvn install -P{profile} 命令打包war
example:     
1、mvn install               没有指定profile,默认为dev
2、mvn install -Ptest -Dmaven.test.skip=true   指定profile为test并跳过测试

在使用tomcat部署时,先使用maven的命令切换至目标环境,然后tomcat的目标目录设置为编译后的target/${project.actifact}目录(大多数IDE比如eclipse和IDEA都是这样)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值