Spring boot项目maven的profile多环境配置不自动替换变量的问题解决

Spring boot项目maven的profile多环境配置不自动替换变量的问题解决
 

在网上找了好久,配置都很简单,可是我的程序就是不能自动替换变量,最终单独测试,发现原来是引用spring boot parent的问题

引用一位博主的话"spring-boot为了保护application.yml和application.properties,修改了默认的占位符${...}为@...@,然后发现resources插件有一个配置项"

<useDefaultDelimiters>true</useDefaultDelimiters>

解决办法:

在build里面加plugin

		<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
 

下面分享完整的配置,一共有两块,一块是profiles配置,一块是build

其中prifiles里面定义的 properties属性会替换.properties文件里面的变量

例如 application.properties里面有一个mapdb.path=${mapdb.path}

会被替换成正确的

	<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>dev/*</exclude>
<exclude>prod/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources/</directory>
<filtering>false</filtering>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

下面是profiles定义

	<profiles>
<profile>
<id>dev</id>
<properties>
<mapdb.path>${basedir}/mapdb</mapdb.path>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile> <profile>
<id>production</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
上一篇:Android为TV端助力linux命令


下一篇:【算法】双指针算法 ( 有效回文串 II )