1. 程式人生 > >maven項目之Profile---不同環境打包不同配置

maven項目之Profile---不同環境打包不同配置

程序員 不同的 fff payment 每次 pom.xml 目錄 麻煩 項目

作為一名程序員,在開發的過程中,經常需要面對不同的運行環境(開發環境、測試環境、生產環境、內網環境、外網環境等等),在不同的環境中,相關的配置一般不一樣,比如數據源配置、日誌文件配置、以及一些軟件運行過程中的基本配置。每次在不同環境部署程序時,都需要修改相應的配置文件,使之完成環境的配置。這麽做存在一個比較大的問題:每次修改配置非常麻煩,而且配置錯誤會產生不可預估的影響,比如,在發布生產環境時用的開發環境的配置還好,但如果在開發環境下用生產環境的數據,將會造成生產數據的汙染,導致生產環境崩潰。

目前JAVA相關的項目基本都是使用Maven來進行構建。在maven中實現多環境的構建可移植性需要使用profile,通過不同的環境激活不同的profile來達到構建的可移植性。

一、pom中的profile配置

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

<profiles>
          <profile>
              <!-- 測試環境 -->
              <id>test</id>
              <properties>
                  <jdbc.username>ludidevelop</jdbc.username>
                  <
jdbc.password>[email protected]</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>[email protected]</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

maven項目之Profile---不同環境打包不同配置