spring profile 與 maven profile 多環境管理

實際開發中一個項目至少對應開發、測試、生成三種環境,如何方便的管理多環境是今天的要討論的主題spring

Spring Profile

Spring Profile 是 Spring 提供的多環境管理方案。apache

如圖,每種環境都對應一個 properties 文件,而後在application.properties中配置一下要使用的環境bash

spring.profiles.active=dev
複製代碼

上面配置匹配的是 application-dev.properties,若是寫的是test,則匹配 application-test.properties。也就是說,Spring Profile 對配置文件的命名有要求,必須是 application- 開頭app

除了配置環境外,一些不隨環境而變化的配置也應該放到 application.properties中,application-.properties最好只存放與環境相關的配置項maven

以上就是 Spring Profile 給出的多環境管理方案。經過改變 spring.profiles.active的值來切換不一樣的環境。這種方法簡單易懂,但有兩個問題。編輯器

  1. 每次切換環境要手動修改 spring.profiles.active 的值
  2. 打包的時候,要手動刪除其它環境的配置文件,否則其它環境的敏感信息就都打包進去了

爲了解決這兩個問題,咱們須要 maven profile 的配合ide

maven profile

  maven 的 profile 可讓咱們定義多套配置信息,並指定其激活條件,而後在不一樣的環境下使用不一樣的profile配置。   spring-boot

profile 的定義位置

在maven中有兩個地方能夠配置 profile測試

  1. pom.xml中:這裏面定義的 profile 做用範圍是當前項目
  2. {user}/.m2/settings.xml中:這裏面定義的 profile 做用範圍是全部使用了該配置文件的項目

settings.xml中的 profile

不一樣的地方 profile 中能定義的信息也不相同ui

  • 因爲settings.xml做用範圍寬泛, profile 中只能定義一些公共信息,以下
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    ...
    <profiles>
        <profile>
            <id>...</id>
            <activation>...</activation>
            <repositories>...</repositories>
        </profile>
    </profiles>
    ...
</settings>
複製代碼
  • id:該 profile 的惟一標識
  • activation:在哪些狀況下激活 profile,這裏面有多種策略可供選擇,只要知足其中一個條件就激活
  • repositories:遠程倉庫

因爲能配置的東西有限,通常都會將 maven profile 配置在pom.xml

pom.xml中 的profile

pom.xml中:profile 能定義的東西就很是多了,以下

<profiles>
    <profile>
        <id>..</id>
        <activation>...</activation>
        <build>...</build>
        <modules>...</modules>
        <repositories>...</repositories>
        <pluginRepositories>...</pluginRepositories>
        <dependencies>...</dependencies>
        <reporting>...</reporting>
        <dependencyManagement>...</dependencyManagement>
        <distributionManagement>...</distributionManagement>
    </profile>
</profiles>
複製代碼

固然咱們的目的也不是把它配全,而是解決 Spring Profile 遺留下來的兩個問題。

首先看第一個問題

問題1

"每次切換環境要手動修改spring.profiles.active的值"

這個問題就能夠經過配置 profile 解決,在pom的根節點下添加

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <!-- activeByDefault 爲 true 表示,默認激活 id爲dev 的profile-->
            <activeByDefault>true</activeByDefault>
        </activation>
        <!-- properties 裏面能夠添加自定義節點,以下添加了一個env節點 -->
        <properties>
            <!-- 這個節點的值能夠在maven的其餘地方引用,能夠簡單理解爲定義了一個叫env的變量 -->
            <env>dev</env>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <env>test</env>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <env>prod</env>
        </properties>
    </profile>
</profiles>
複製代碼

如上,定義了三套環境,其中id爲dev的是默認環境,三套環境中定義了叫 env的「變量」

若是你用的是idea編輯器,添加好後,maven控件窗口應該會多出一個 Profiles,其中默認值就是上面配置的dev

最小化的 profiles 已經配置好了,經過勾選上圖中的Profiles,就能夠快速切換 maven的 profile 環境。

如今 maven profile 能夠經過 勾選上圖中的Profiles 快速切換環境

Spring Profile 還得經過 手動修改spring.profiles.active的值來切環境

如今的問題是怎樣讓 maven profile的環境與Spring Profile一一對應,達到切換maven profile環境時,Spring Profile環境也被切換了

還記得maven profile 中定義的 env "變量"嗎,如今只須要把

spring.profiles.active=dev
複製代碼

改爲

spring.profiles.active=@env@
複製代碼

就將maven profile 與 Spring Profile 環境關聯起來了

當maven profile 將 環境切換成 test 時,在pom中定義的id爲test的profile環境將被激活,在該環境下env的值是test,maven插件會將 @env@ 替換爲 test,這樣Spring Profile的環境也隨之發生了改變。從上面能夠看出,自定義的"變量"env的值還不能亂寫,要與Spring Profile的環境相對應。

總結

  • 第一步,在pom文件中配置 profiles
  • 第二步,在application.properties配置文件中添加 spring.profiles.active=@env@

問題2

打包的時候,要手動刪除其它環境的配置文件,否則其它環境的敏感信息就都打包進去了

解決這個問題須要在pom根節點下中配置 build 信息

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <!--先排除application開頭的配置文件-->
                <exclude>application*.yml</exclude>
            </excludes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <!--filtering 須要設置爲 true,這樣在include的時候,纔會把
            配置文件中的@env@ 這個maven`變量`替換成當前環境的對應值  -->
            <filtering>true</filtering>
            <includes>
                <!--引入所需環境的配置文件-->
                <include>application.yml</include>
                <include>application-${env}.yml</include>
            </includes>
        </resource>
    </resources>
    <!--<plugins>-->
    <!--    <plugin>-->
    <!--        <groupId>org.springframework.boot</groupId>-->
    <!--        <artifactId>spring-boot-maven-plugin</artifactId>-->
    <!--    </plugin>-->
    <!--</plugins>-->
</build>
複製代碼
  • directory:資源文件所在目錄
  • includes:須要包含的文件列表
  • excludes:須要排除的文件列表

如上,配置了兩個 <resource>,第一個先排除了src/main/resources目錄下全部 application 開頭是配置文件,第二個在第一個的基礎上添加了所需的配置文件。注意 application-${env}.yml,它是一個動態變化的值,隨着當前環境的改變而改變,假如當前環境是 id叫 dev的 profile,那麼env的值爲 dev。

這樣配置後,maven在build時,就會根據配置先排除掉指定的配置文件,而後根據當前環境添加所須要的配置文件。

相關文章
相關標籤/搜索