springboot區分開發、測試、生產多環境的應用配置(二)


轉:https://www.jb51.net/article/139119.htm

springboot區分開發、測試、生產多環境的應用配置(二)

這篇文章主要給大家介紹了關於maven profile自動切換環境參數的2種方法,文中通過示例代碼將這兩種方法介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨着小編來一起學習學習吧。

一、前言

在開發過程中,我們經常會根據不同的環境配置不同的參數,如數據源的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

 jdbc.username=dev
 jdbc.password= 123456
    policy.environment=dev

application-test.properties

 jdbc.username=test
 jdbc.password= 888888
    policy.environment=test

application-pro.properties

 jdbc.username=root
 jdbc.password= 666666
policy.environment=pro

第二步:定義總的屬性文件application.properties,該文件中的值去引用application-<env>.properties中的key

application.properties

spring.profiles.active= @policy.environment@

第三步:配置profile

<profiles>
<profile>
<!-- 開發環境 -->
<id>dev</id>
<properties>
<env>dev</env><!-- 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>

打包運行

1
2
3
4
5
// 如果不指定環境,默認是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的值。

 

實現原理: 

在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

?
1
2
jdbc.username=dev
jdbc.password= 123456

env/test/config.properties

?
1
2
jdbc.username=test
jdbc.password= 888888

env/pro/config.properties

?
1
2
jdbc.username=root
jdbc.password= 666666

第二步:創建一個與環境無關的application.properties

application.properties

?
1
2
# 公共配置
salt= 123456789

第三步:配置profiles

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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方式在打包時只打包指定環境的配置文件,可以將各種文件放到各自的環境文件夾中,在打包的時候會將整個文件夾都打包進去。推薦此種方式

 


免責聲明!

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



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