maven pom.xml加載不同properties配置[轉]


可以參考http://www.openwebx.org/docs/autoconfig.html 

1.pom.xml

 

===========================

 

<!-- 不同的打包環境配置: test=開發/測試測試環境,  product=生產環境; 命令行方式: mvn clean install -Dmaven.test.skip=true -Ptest 或 -Pproduct-->    

    <profiles>
       <!-- 開發/測試環境,默認激活 -->
       <profile>
           <id>test</id>
           <properties>
              <env>test</env>
           </properties>
           <activation>
              <activeByDefault>true</activeByDefault><!--默認啟用的是dev環境配置-->
           </activation>
       </profile>
       <!-- 預發布環境 -->
       <profile>
           <id>preproduction</id>
           <properties>
              <env>preproduction</env>
           </properties>
       </profile>
       <!-- 生產環境 -->
       <profile>
           <id>production</id>
           <properties>
              <env>production</env>
           </properties>
       </profile>
    </profiles>  
  
      <build>
        <filters> <!-- 指定使用的 filter -->
          <filter>src/main/filters/filter-${env}-env.properties</filter>
        </filters>
        <resources>
          <resource> <!-- 配置需要被替換的資源文件路徑, db.properties 應該在 src/main/resource 目錄下 -->
            <directory>src/main/resources</directory>
            <filtering>true</filtering> <!-- 是否使用過濾器 -->
          </resource>
        </resources>

      </build>

 

 

 

2.src/main/filters/下不同環境的配置文件

 

src/main/filters/filter-preproduction-env.properties

 

src/main/filters/filter-production-env.properties

 

src/main/filters/filter-test-env.properties

 

======filter-test-env.properties 舉例

 

jdbc.url=jdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=testuser
jdbc.password=123456

 

 

 

3.src/main/resources下要應用處理的文件

 

src/main/resources/conf/db.properties

 

======db.properties

jdbc.datasource=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}

jdbc.minPoolSize=10
jdbc.maxPoolSize=50
jdbc.autoCommit=false

 

-----------------------------------

自定義配置屬性:

Custom properties in the POM

User defined properties in the pom.xml.

<project>
...
  <properties>
     <my.filter.value>hello</my.filter.value>
  </properties>
...
</project>
  • ${my.filter.value } will result in hello if you inserted the above XML fragment in your pom.xml

再比如:在pom.xml中properties自定義如下:

Xml代碼   收藏代碼
  1. <properties>  
  2.         <springframework_version>3.1.0.RELEASE</springframework_version>  
  3.         <maven.bbs.appConfig>itConfig.xml</maven.bbs.appConfig>  
  4.     </properties>  

 

在web容器加載的時候,假如spring監聽的配置文件是:applicationContext.xml,那么applicationContext.xml文件內容可以這樣寫來引入maven的properties屬性值:

<import resource="${maven.bbs.appConfig}"/>

maven編譯過后,targer項目里面的applicationContext.xml里面相應的引入就會變成

<import resource="itConfig.xml"/>

 

 

 

<build>
<!-- autoconfig with antx.properties -->
<filters>
<filter>${filterFile}</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

<profiles>
<profile>
<id>dev</id>
<properties>
<filterFile>antx.properties</filterFile>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>




現在發現有兩種可以實現配置
一種是使用filter
第二種是使用
autoconfig-maven-plugin  來產生autoconfig


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM