问题:之前的mvn多模块项目,我会在parent模块下,创建几个filter配置文件,以实现不同环境不同配置。但是在搭建springboot的mvn多模块框架时,配置文件始终替换不了。
1.png
原因:仔细看springboot的官方文档
https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#using-boot-maven-plugin
Note that, since the application.properties and application.yml files accept Spring style placeholders ({…}), the Maven filtering is changed to use @..@ placeholders. (You can override that by setting a Maven property called resource.delimiter.) 是说springboot自己使用了{…}占位符,所以mvn打包的占位符使用@@
顺手上一下示例代码:
parent的pom中增加:
<profiles>
<profile>
<id>production</id>
<properties>
<env>prod</env>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
增加build配置:
<build>
<filters>
<filter>../parent/src/main/filters/${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>/.ftl</exclude>
<exclude>/-dynamic.xml</exclude>
<exclude>/mybatis//.xml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>/.ftl</include>
<include>/-dynamic.xml</include>
<include>/mybatis//.xml</include>
</includes>
</resource>
</resources>
使用:
其他子模块的yml中
url: @config.datasource.url@
解决:占位符改为使用@@ 问题解决。
官方文档没事还是要仔细看看,尤其是note不能省。
网友评论