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

做爲一名程序員,在開發的過程當中,常常須要面對不一樣的運行環境(開發環境、測試環境、生產環境、內網環境、外網環境等等),在不一樣的環境中,相關的配置通常不同,好比數據源配置、日誌文件配置、以及一些軟件運行過程當中的基本配置。每次在不一樣環境部署程序時,都須要修改相應的配置文件,使之完成環境的配置。這麼作存在一個比較大的問題:每次修改配置很是麻煩,並且配置錯誤會產生不可預估的影響,好比,在發佈生產環境時用的開發環境的配置還好,但若是在開發環境下用生產環境的數據,將會形成生產數據的污染,致使生產環境崩潰。html

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

1、pom中的profile配置程序員

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

<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打包的時候也是讀這個默認配置)api

同時每一個profile還定義了多個屬性,其中profiles.active表示被激活的profile的配置文件的目錄。tomcat

2、在pom文件的build中配置eclipse

在某個resource中若是設置filtering爲true,將會根據輸入參數動態修改相關內容async

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!-- 在某個resource中若是設置filtering爲true,將會根據輸入參數動態修改相關內容  -->
        <filtering>true</filtering>
    </resource>
</resources>

2、profile屬性引用maven

profile中定義的屬性,能夠在.properties中用"${}"直接應用ide

下面貼一下在properties問價中怎麼引用profile屬性:

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

4、maven打包

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

 

參考文獻:

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

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

相關文章
相關標籤/搜索