maven项目之Profile---不同环境打包不同配置

作为一名程序员,在开发的过程中,经常需要面对不同的运行环境(开发环境、测试环境、生产环境、内网环境、外网环境等等),在不同的环境中,相关的配置一般不一样,比如数据源配置、日志文件配置、以及一些软件运行过程中的基本配置。每次在不同环境部署程序时,都需要修改相应的配置文件,使之完成环境的配置。这么做存在一个比较大的问题:每次修改配置非常麻烦,而且配置错误会产生不可预估的影响,比如,在发布生产环境时用的开发环境的配置还好,但如果在开发环境下用生产环境的数据,将会造成生产数据的污染,导致生产环境崩溃。

目前JAVA相关的项目基本都是使用Maven来进行构建。在maven中实现多环境的构建可移植性需要使用profile,通过不同的环境激活不同的profile来达到构建的可移植性。

一、pom中的profile配置

首先是profile配置,在pom.xml中添加如下profile的配置:

<profiles>
          <profile>
              <!-- 测试环境 -->
              <id>test</id>
              <properties>
                  <jdbc.username>ludidevelop</jdbc.username>
                  <jdbc.password>Ludi@12345</jdbc.password>
                  <jdbc.jdbcUrl>jdbc:mysql://10.77.55.161:3306/quicksureuat?allowMultiQueries=true</jdbc.jdbcUrl>
                  <log.file.address>../logs/quickSureMobileServer.log</log.file.address>
                  <exception.file.address>../logs/quickSureMobileServerError.log</exception.file.address>
                  <jy.connect.username>xxbao1113_test</jy.connect.username>
                  <jy.connect.password>xxbao1113_test</jy.connect.password>
                  <sinosafe.synchronization.url>http://mtest.sinosafe.com.cn/quicksurepayment</sinosafe.synchronization.url>
                  <sinosafe.asynchronous.url>http://mtest.sinosafe.com.cn/quicksurepolicy</sinosafe.asynchronous.url>
                  <sinosafe.return.url>http://localhost:8989/ludiquickSureMobileServer/paymentCompleteServlet/goPaymentSucessPage.do</sinosafe.return.url>
              </properties>
              <!-- 默认环境,tomcat打包的时候也是读这个默认配置 -->
              <activation>
                  <activeByDefault>true</activeByDefault>
              </activation>
          </profile>
          <profile>
              <!-- 生产环境 -->
              <id>product</id>
              <properties>
                  <jdbc.username>quicksuredb%ludi</jdbc.username>
                  <jdbc.password>quicksure@168</jdbc.password>
                  <jdbc.jdbcUrl>jdbc:mysql://quicksuredb.mysqldb.chinacloudapi.cn:3306/quicksuredbpe?allowMultiQueries=true</jdbc.jdbcUrl>
                  <log.file.address>/data/logs/quickSureMobileServer.log</log.file.address>
                  <exception.file.address>/data/logs/quickSureMobileServerError.log</exception.file.address>
                  <jy.connect.username>xxbao16328</jy.connect.username>
                  <jy.connect.password>xxb!^#*16328</jy.connect.password>
                  <sinosafe.synchronization.url>http://m.sinosafe.com.cn/quicksurepayment</sinosafe.synchronization.url>
                  <sinosafe.asynchronous.url>http://m.sinosafe.com.cn/quicksurepolicy</sinosafe.asynchronous.url>
                  <sinosafe.return.url>http://m.quicksure.com/ludiquickSureMobileServer/paymentCompleteServlet/goPaymentSucessPage.do</sinosafe.return.url>
              </properties>
          </profile>
    </profiles>

 

这里定义了两个环境,test(测试环境)、product(生产环境),其中开发环境是默认激活的(activeByDefault为true),这样如果在不指定profile时默认是开发环境。(默认环境,eclipse中tomcat打包的时候也是读这个默认配置)

同时每个profile还定义了多个属性,其中profiles.active表示被激活的profile的配置文件的目录。

二、在pom文件的build中配置

在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!-- 在某个resource中如果设置filtering为true,将会根据输入参数动态修改相关内容  -->
        <filtering>true</filtering>
    </resource>
</resources>

二、profile属性引用

profile中定义的属性,可以在.properties中用"${}"直接应用

下面贴一下在properties问价中怎么引用profile属性:

jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.jdbcUrl=${jdbc.jdbcUrl}

四、maven打包

maven打包时命令:mvn clean package -P profileid

 

参考文献:

http://www.cnblogs.com/lzxianren/p/maven-profile.html

http://elim.iteye.com/blog/1900568

 

posted @ 2017-05-27 11:44  编码龟  阅读(465)  评论(0编辑  收藏  举报