Spring-Boot application.yml 文件拆分,實現 maven 多環境動態啓用 Profiles

在實際使用環境中,咱們同一個應用環境可能須要在不一樣環境運行(開發、測試、生產等),每一個環境的參數都有可能不一樣(鏈接參數、日誌級別等),使用 Spring-Boot 的 Profiles 能夠將不一樣環境下的參數進行拆分,並指定加載。mysql

1. 使用 Spring-Boot Profiles 拆分 application.yml 文件

首先咱們有一個很是簡單的 application.yml 文件spring

database: mysql
spring:
    datasource:
        url : jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8
        username : root
        password : 12345678
複製代碼
Yml

咱們如今有開發環境(dev)、測試環境(test)、生產環境(prod)sql

修改application.yml 文件apache

database: mysql

spring:
  profiles:
    active: dev

#這兩行實際沒意義,若是有其餘公共參數能夠補充進來
spring:
    datasource:

#開發環境
spring:
  profiles: dev

spring:
    datasource:
        url : jdbc:mysql://127.0.0.1:3306/dev?characterEncoding=UTF-8
        username : root
        password : 12345678

#測試環境
spring:
  profiles: test

spring:
    datasource:
        url : jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8
        username : root
        password : 12345678

#生產環境
spring:
  profiles: prod

spring:
    datasource:
        url : jdbc:mysql://127.0.0.1:3306/prod?characterEncoding=UTF-8
        username : root
        password : 12345678
複製代碼
Yml

咱們只須要在 spring.profiles.active 指定使用的 profilesbash

固然目前來講,這種配置文件並不符合咱們的需求,咱們須要把不一樣環境的參數放到不一樣的文件中。app

經過與配置文件相同的命名規範,建立 application-{profiles}.yml 文件 存放不一樣環境的參數。maven

修改 application.ymlspring-boot

database: mysql

spring:
  profiles:
    active: dev

#這兩行實際沒意義,若是有其餘公共參數能夠補充進來
spring:
    datasource:
複製代碼
Yml

建立 application-dev.yml測試

spring:
    datasource:
        url : jdbc:mysql://127.0.0.1:3306/dev?characterEncoding=UTF-8
        username : root
        password : 12345678
複製代碼
Yml

2. 啓動參數

若是 spring.profiles.active 沒有指定值,那麼只會使用沒有指定 spring.profiles 的值,也就是隻會加載通用的配置。

而且咱們能夠在啓動應用程序的時候,對 spring.profiles.active 進行指定,這樣就不須要每次修改環境都須要手動修改配置文件再編譯。

例如運行 Jar 包,指定使用 dev 環境參數:

java -jar xxx.jar --spring.profiles.active=dev
複製代碼
Shell

3. 使用 Maven 在打包時只加載特定的 Profiles 配置文件

因爲各類各樣的緣由,咱們確定但願打出哪一個環境的包,就只須要包含這個環境的配置文件便可,不想包含其餘環境的配置文件,這時候能夠直接在 maven 中使用 profilesresources 來配置,打包時使用 mvn package -P dev 便可,仍然使用 java -jar xxx.jar --spring.profiles.active=dev 來運行。

<profiles>
    <!--開發環境-->
    <profile>
        <id>dev</id>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!--測試環境-->
    <profile>
        <id>test</id>
        <properties>
            <spring.profiles.active>test</spring.profiles.active>
        </properties>
    </profile>
    <!--生產環境-->
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>prod</spring.profiles.active>
        </properties>
    </profile>
</profiles>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/resources.${spring.profiles.active}</directory>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
複製代碼
XML

4. 使用 Maven 在打包時自動通知 Spring-Boot 加載的 Profiles 環境參數

雖然每次均可以打包成相應配置文件的 Jar 包,可是每次運行,都得用 --spring.profiles.active 參數來指定運行環境,打包時指定一次,運行時指定一次,確實很麻煩。

不自動不舒服,下面咱們就來看看怎麼把這個參數能夠省略掉。

首先咱們得知道,加這個參數的緣由就是想着項目中存在多個環境,這個環境是須要在 application.yml 中配置,或者運行時經過參數指定。

實際上咱們在使用 maven 打包時就能肯定咱們所選的運行環境,所須要的配置文件,那麼咱們不想在運行時經過參數來指定,那麼咱們可不能夠修改 application.yml 配置文件呢,這樣就不須要咱們運行時來指定參數了吧。

下面就介紹兩種方法來實現這個功能:

4.1. 方法1:利用 maven filter 來替換標記的內容

這個方法直接在上一步的基礎上,增長一個佔位符標記,而且在 resource 中將過濾器打開。

修改 pom.xml 文件

<!-- profiles部分不變 -->
<properties>
    <resource.delimiter>#</resource.delimiter>
</properties>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>src/main/resources.${spring.profiles.active}</directory>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>
複製代碼
XML

修改 application.yml 文件

spring:
    profiles:
        active: #spring.profiles.active#
複製代碼
Yml

這裏咱們經過 <resource.delimiter>#</resource.delimiter> 重寫定義了一個佔位符,其實熟悉 maven 的朋友都知道,maven 自己有自帶的佔位符 ${...} ,可是在 Spring-Boot 中不行,這個佔位符被 Spring-Boot 佔用了,因此咱們就得從新定義一遍。再經過 <filtering>true</filtering> 這樣的一個設置,打開過濾器開關,這樣 application.yml 文件中的 #spring.profiles.active# 部分就會替換爲 pom.xmlprofiles 中定義的 spring.profiles.active 變量值。

4.2. 方法2:利用 maven-resources-plugin 插件

maven 的 maven-resources-plugin 插件使用方法也是很簡單的,首先,幹掉咱們上面所用的 <build><resources></resources></build> 這插件就是用來代替他的高級版。

接着我這裏有一個示例,一看就懂的那種。

修改 pom.xml 文件

<!-- profiles部分不變 -->
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>default-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/classes</outputDirectory>
                        <useDefaultDelimiters>false</useDefaultDelimiters>
                        <delimiters>
                            <delimiter>#</delimiter>
                        </delimiters>
                        <resources>
                            <resource>
                                <directory>src/main/resources/</directory>
                                <filtering>true</filtering>
                            </resource>
                            <resource>
                                <directory>src/main/resources.${spring.profiles.active}</directory>
                                <filtering>false</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
複製代碼
XML

修改 application.yml 文件

spring:
    profiles:
        active: #spring.profiles.active#
複製代碼
Yml

注意看 pom.xml 中的 <delimiter>#</delimiter> 定義了一個佔位符,能夠在複製文件的時候,在文件中查找 #...# 將其中的內容進行替換。

經過以上兩種方法修改後,再次運行 mvn package -P dev 打包,將生成的 Jar 包用 RAR 解壓,直接查看 application.yml 文件,咱們能夠看到原來的 #spring.profiles.active# 已經被替換成了 dev ,這樣在運行 Jar 包時就不須要進行參數指定,直接運行 java -jar xxx.jar 便可。

參考連接

Spring Boot教程 – Spring Boot Profiles實現多環境配置切換

如何在@SpringBootTest中動態地啓用不一樣的profiles

spring boot經過maven filter替換properties屬性(多環境配置)

相關文章
相關標籤/搜索