maven profile自動切換環境引數的兩種實現方式
簡介
在開發過程中,我們經常會根據不同的環境配置不同的引數,如資料來源的ip,username,password、url、祕鑰等都會不同,傳統方式是在一個配置檔案中通過修改properties檔案中的引數值或者通過註釋解註釋來達到目的,這樣不僅容易出錯,還浪費不必要的時間,更重要的是把程式碼釋出到測試環境或者生產環境還容易忘記改。為解決這種問題,maven提供了一種解決方案,就是profile。
下圖為傳統方式,需要來回的註釋和解註釋
profile定義的位置
- 針對於特定專案的profile配置我們可以定義在該專案的pom.xml中。
- 針對於特定使用者的profile配置,我們可以在使用者的settings.xml檔案中定義profile。該檔案在使用者家目錄下的“.m2”目錄下。
- 全域性的profile配置。全域性的profile是定義在Maven安裝目錄下的“conf/settings.xml”檔案中的。
filter方式實現
- 第一步:分別定義application-dev.properties、application-test.properties、application-pro.properties三個檔案
application-dev.properties
env.jdbc.username=dev
env.jdbc.password=123456
application-test.properties
env.jdbc.username =test
env.jdbc.password=888888
application-pro.properties
env.jdbc.username=root
env.jdbc.password=666666
- 第二步:定義總的屬性檔案application.properties,該檔案中的值去引用application-<env>.properties中的key
application.properties
// 引用application-<env>中的key
jdbc.username=${env.jdbc.username}
jdbc.password =${env.jdbc.password}
# 公共配置
salt=123456789
- 第三步:配置profile
<profiles>
<profile>
<!-- 開發環境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 設定預設啟用這個配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 測試環境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!-- 釋出環境 -->
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
第四步:配置filter和resource
${env}就是在mvn package -P <env>的名字,這樣就告訴application.properties中應用的key是那個屬性檔案的key了
<build>
<finalName>profile-app</finalName>
<!-- 定義了變數配置檔案的地址 -->
<filters>
<filter>src/main/resources/config/application/application-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
- 打包執行
// 如果不指定環境,預設是activeByDefault=true的環境,當前是指開發環境
mvn package
// 打包指定的環境通過-P 引數,注意p是大寫的
mvn package -P <env>
從mvn packege -P test執行的結果中可以看到生成的target目錄下classes/application.perperties中的jdbc.username和jdbc.password 就是application-test.properties中配置的env.jdbc.username和env.jdbc.password的值。
在spring中如果要使用屬性配置檔案,直接引入這個總的配置檔案即可,其他的環境配置檔案的使命已經結束了。
<context:property-placeholder location="classpath:application.properties"/>
實現原理:
在pom.xml中為每個不同的環境定義不同的profile,每個profile都有一個環境名稱,然後為不同環境定義不同的配置檔案(如application-<env>.properties), 再定義一個總的屬性檔案(如application.properties), 然後讓application.properties的value去引用application-<env>.properties中對應的key,在打包時指定要打包的環境的名稱即可,這樣application.properties中的key的值就是相對應環境application-<env>.properties對應的值了。
多resource實現方式
步驟
- 第一步:在src/main/resource建立一個env目錄,再建立各個環境的子目錄,再再各個環境子目錄下建立名為config.properties的檔案,每個鍵相同,值不同。
env/dev/config.properties
jdbc.username=dev
jdbc.password=123456
env/test/config.properties
jdbc.username=test
jdbc.password=888888
env/pro/config.properties
jdbc.username=root
jdbc.password=666666
- 第二步:建立一個與環境無關的application.properties
application.properties
# 公共配置
salt=123456789
- 第三步:配置profiles
<profiles>
<profile>
<!-- 開發環境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 設定預設啟用這個配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 測試環境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!-- 釋出環境 -->
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
- 第四步:配置resource
<build>
<finalName>profile-app</finalName>
<!-- 定義了變數配置檔案的地址 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>env/dev/*</exclude>
<exclude>env/test/*</exclude>
<exclude>env/pro/*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/env/${env}</directory>
<includes>
<include>*.*</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
- 第五步:執行 mvn package -P test
如果經常使用mvn package -P <env>可以在idea中配置一些maven, 步驟為:Edit Configurations… —– + —- Maven —- 分別為每個環境新增maven 命令,以後雙擊Run Configureations中的任意一個就是相當於執行mvn package -P <env>命令了
兩種方式比較
filter方式會把所有的application-dev.properties、application-test.properties、application-pro.properties檔案都會打包進去,而且此種方式只能針對屬性檔案,如果有其他檔案(如.xml)也根據不同的環境有不同的配置,這種方式是不好處理。
多resource方式在打包時只打包指定環境的配置檔案,可以將各種檔案放到各自的環境資料夾中,在打包的時候會將整個資料夾都打包進去。推薦此種方式