spring boot通过maven filter替换properties属性(多环境配置)

时间:2023-02-13 18:26:58

这两天项目到了差不多收尾了,想把各种环境配置通过maven filter在maven打包的时候就替换掉properties的配置。之前一直用${my.properies}的方式,打包了以后就是替换不掉properties里的属性。
这是我的maven filter的属性

filter.spring.freemarker.template-loader-path=file:/mnt/web/ftl/
filter.spring.resources.static-locations=file:/mnt/web/static/
filter.server.port=80
#mysql connection info
filter.jdbc.url=jdbc:mysql://192.168.1.111:3306/ubip?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true
filter.jdbc.username=root
filter.jdbc.password=banger

#redis config
filter.redis.database=0
filter.redis.host=192.168.1.111
filter.redis.port=6379
filter.redis.password=1234

在application.properties文件里需要被替换的属性

jdbc.url=${filter.jdbc.url}
...

maven配置

    <profiles>
<profile>
<id>product</id>
<properties>
<env>product</env>
</properties>
</profile>
</profiles>
<build>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/${env}.properties</filter>
</filters>
<resources>
<resource> <!-- 配置需要被替换的资源文件路径, properties 应该在 src/main/resource 目录下 -->
<directory>src/main/resources</directory>
<excludes>
<exclude>static/**</exclude>
<exclude>ftl/**</exclude>
</excludes>
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build>

以前这样配置都是可以的。在spring boot里就不行。后来看了官方文档,里面有这样一句话

13.2 Maven
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:

Java 1.6 as the default compiler level.
UTF-8 source encoding.
A Dependency Management section, allowing you to omit tags for common dependencies, inherited from the spring-boot-dependencies POM.
Sensible resource filtering.
Sensible plugin configuration (exec plugin, surefire, Git commit ID, shade).
Sensible resource filtering for application.properties and application.yml including profile-specific files (e.g. application-foo.properties and application-foo.yml)
On the last point: since the default config files accept Spring style placeholders (${…​}) the Maven filtering is changed to use @..@ placeholders (you can override that with a Maven property resource.delimiter).

大致的意思是我的maven继承了spring-boot-starter-parent,并且spring默认配置文件接受的占位符也是 ...mavenfilter {}占位符就被spring的maven pom替换掉了,变成了@..@,我们可以通过resource.delimiter来覆盖它。
看下spring-boot-starter-parent 这个pom里写这一段

<properties>
<java.version>1.6</java.version>
<resource.delimiter>@</resource.delimiter> <!-- delimiter that doesn't * with Spring ${} placeholders -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>

那一切就清楚了,我将properties中的占位符替换下变成

#mysql connection info
jdbc.url=@filter.jdbc.url@
jdbc.username=@filter.jdbc.username@
jdbc.password=@filter.jdbc.password@

#redis config
# Redis数据库索引(默认为0)
redis.database=@filter.redis.database@
# Redis服务器地址
redis.host=@filter.redis.host@
# Redis服务器连接端口
redis.port=@filter.redis.port@
# Redis服务器连接密码(默认为空)
redis.password=@filter.redis.password@

这样maven就能替换属性了。