maven多环境配置打包(自定义properties)

项目开发经常碰到配置文件中测试的jdbc.url和线上的是不一样的。为此每次打包都要修改配置文件,很烦,而且也容易出错。 

最近才发现,原来用maven的profile可以实现多种环境的配置。 

1、在src/main/resources/filters,建两个文件:test.properties和prod.properties。 
分别放置测试环境和生产环境的配置。(假设两个文件中都设置了jdbc.url属性) 

2、新建src/main/resources/conf.properties文件。里面设置 
Properties代码   收藏代码
  1. jdbc.url=${jdbc.url}  

3、配置pom.xml。配置如下 
Pom.xml代码   收藏代码
  1. <profiles>  
  2.         <profile>  
  3.             <id>test</id>  
  4.             <properties>  
  5.                 <env>test</env>  
  6.             </properties>  
  7.             <activation>  
  8.                 <activeByDefault>true</activeByDefault>  
  9.             </activation>  
  10.         </profile>  
  11.         <profile>  
  12.             <id>prod</id>  
  13.             <properties>  
  14.                 <env>prod</env>  
  15.             </properties>  
  16.         </profile>  
  17.     </profiles>  
  18.   
  19.     <build>  
  20.         <filters>  
  21.             <filter>src/main/resources/filters/${env}.properties</filter>  
  22.         </filters>  
  23.         <resources>  
  24.             <resource>  
  25.                 <directory>src/main/resources</directory>  
  26.                 <filtering>true</filtering>  
  27. <includes>
    <include>**/*</include>
    </includes>
  28.             </resource>  
  29.         </resources>  
  30. <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>
    <encoding>UTF-8</encoding>
    </configuration>
    </plugin>
     
  31. </plugins>
  32.     </build>  

4、打包。使用mvn packge -Ptest 即可实现测试环境打包。如果要prod环境配置,只需-Pprod即可。当然pom配置中默认不传-P参数的话是使用test环境配置。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值