Maven profile 与 Spring profile 联动实现 profile 配置
1. Spring profile: 使用特定于profile的配置文件
如果你正在使用application.properties,可以创建额外的属性文件,遵循application-{profile}.properties这种命名格式,这样就能提供特定于profile的属性了。
如果使用yaml来配置属性,则可以遵循与配置文件相同的命名规范,即创建application-{profile}.yml这样的配置文件,并将与profile无关的属性继续放在application.yml里。
既然使用了yaml,还可以把所有profile的配置属性都放在一个application.yml文件里。
下图启用了三个dev, test, production三个profile配置,基于不同的profile配置可以设置不同的日志输出级别。这里设置dev环境下org.springframework的日志输出级别为debug。而test和production环境下的日志输出级别为info。
也可以将三个profile的属性都配置到application.yaml文件里,不同的profile配置通过三个连字符(—)分割。如下:
# 更改配置文件名称
logging:
# log4j2配置:classpath:log4j2-config.xml
config: classpath:logging-config.xml
---
spring:
profiles: dev
logging:
# 通过配置文件改变指定包的日志输出级别
level:
org:
springframework: debug
---
spring:
profiles: test
logging:
# 通过配置文件改变指定包的日志输出级别
level:
org:
springframework: info
---
spring:
profiles: production
logging:
# 通过配置文件改变指定包的日志输出级别
level:
org:
springframework: warn
2. 激活Spring profile
通过设置spring.profiles.active属性来激活profile配置。
spring:
profiles:
active: dev
3. 通过Maven profile配置激活Spring profile
在通过Maven进行项目构建时,我们希望在针对不同的环境进行打包时自动变更spring.profiles.active属性的值,这样就需要实现maven profile和spring profile的联动。
3.1 pom.xml中增加profile配置
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- 设置默认激活 -->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<runenv>dev</runenv>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<runenv>test</runenv>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<runenv>production</runenv>
</properties>
</profile>
</profiles>
3.2 资源过滤时替换默认占位符
3.2.1 默认占位符
Set of delimiters for expressions to filter within the resources. These delimiters are specified in the form beginToken*endToken. If no * is given, the delimiter is assumed to be the same for start and end.
So, the default filtering delimiters might be specified as:
<delimiters>
<delimiter>${*}</delimiter>
<delimiter>@</delimiter>
</delimiters>
Since the @ delimiter is the same on both ends, we don’t need to specify @*@ (though we can).
3.2.2 spring boot资源过滤配置
对application*.yml, application*.yaml, application*.properties进行资源过滤,占位符使用@
<properties>
<resource.delimiter>@</resource.delimiter>
</properties>
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>**/application*.yml</exclude>
<exclude>**/application*.yaml</exclude>
<exclude>**/application*.properties</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>
<delimiter>${resource.delimiter}</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
3.3 变更spring.profiles.active配置
spring:
profiles:
active: @runenv@