maven 根據profile,resources,filters來區分部署環境


    項目過程中,在不同的階段,分別需要部署開發環境,測試環境,線上環境。如果都用一套配置文件,很容易弄亂,所以維持多套配置文件很有必要。

    maven提供了一組屬性以供開發人員靈活搭配,可以根據環境來打包,比如測試環境:mvn package -DskipTests -P test,-P也就是指定profile里面id為test的子項配置來打包。在pom文件里面,可以指定多套配置文件,下面例子中區分了三套配置文件,不指定-P則默認為dev。其中的env相當於屬性文件中的env=test,也可以多指定多個屬性,看實際需要。至於為什么不在相應的屬性文件中添加類似env這樣的配置,下文會提到。

 1   <!-- 區分環境打包 -->
 2   <profiles>
 3       <!-- 開發環境[默認] -->
 4       <profile>
 5           <id>dev</id>
 6           <properties>
 7               <env>dev</env>
 8           </properties>
 9           <activation>
10               <activeByDefault>true</activeByDefault>
11           </activation>
12       </profile>
13       <!-- 測試環境 -->
14       <profile>
15           <id>test</id>
16           <properties>
17               <env>test</env>
18           </properties>
19       </profile>
20       <!-- 線上環境 -->
21       <profile>
22           <id>product</id>
23           <properties>
24               <env>product</env>
25           </properties>
26       </profile>
27   </profiles>
View Code

    resources和filters,maven中的這兩個屬性通常是搭配起來使用的,filter這里可以理解為篩選器或者填充器。filters里面可以配置多個filter,但是會以最后一個為准,所以一般只配置一個filter,看下面例子。例子有${env},打包的時候,根據-P test指定,-P 后面的參數與<profile>的<id>一一對應,再根據此ID,到<profile> 的 <properties>得到env屬性值,即<env>test</env>。上文指定了三套配置,所以項目里面也需要有這三個配置文件:dev.properties , test.properties , product.properties

    <filters>
        <filter>src/main/resources/${env}.properties</filter>
    </filters>

    然后是resources

  1. directory屬性指定資源文件放置的目錄。
  2. includes則是指定打包時,需要打到jar/war包里的配置文件。下面例子是說需要把src/main/resources目錄下的所有的xml配置文件和init.properties屬性文件打到包里,所以dev.properties,test.properties,product.properties這三個屬性文件不會出現在[jar/war]包文件里
  3. filtering如果設置為false的話,則表示上文的filters配置失效;如果設置為true,則會根據${env}.properties里面的鍵值對來填充includes指定文件里的${xxxx}占位符。
    <!-- 指定資源文件目錄 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 設置為true,則init.properties會根據${env}.properties里面的配置來填充 -->
            <filtering>true</filtering>
            <includes>
                <include>*.xml</include>
                <include>init.properties</include>
            </includes>
        </resource>
    </resources>

     最后演示一下打包運行的結果,假設init.properties里有這么幾行:

###########################mysql#################################################
pool.driverClassName=${pool.driverClassName}
pool.url=${pool.url}
pool.username=${pool.username}
pool.password=${pool.password}
pool.maxWait=${pool.maxWait}
# env的屬性在這里也有效
env=${env}

 

test.properties屬性文件里是如下配置,注意沒有pool.maxWait配置

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456

則運行打包命令[mvn package -DskipTests -P test]之后,init.properties會被填充為:

###########################mysql#################################################
pool.driverClassName=com.mysql.jdbc.Driver
pool.url=jdbc:mysql://192.168.100.23:3306/etmis
pool.username=root
pool.password=123456
pool.maxWait=${pool.maxWait}
# env的屬性在這里也有效
env=test
pool.maxWait不會被填充,因為在test.properties屬性文件里面沒有這個鍵值對。在xml文件里面的篩選填充也是一樣,會根據文件中的${xxxx}占位符來篩選處理。
<profile>里面配置的屬性和${env}.properties里面的屬性如果有沖突,比如test.properties里面也配置了【env=testaaaa】,會優先使用<profile>里面的



免責聲明!

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



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