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属性(多环境配置)

相关文章
相关标签/搜索