1.配置资源文件中的变量分隔符(标识符 )
<delimiters>@</delimiters>
需要用以下配合,达到替换环境参数:dev或test、prod
<filtering>true</filtering>
能让maven中<profiles>中定义的变量注入到yml或properties文件中,使用如下:
POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>
<finalName>test-project</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<delimiters>@</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>${encoding-charset}</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
<filtering>true</filtering>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
<tt>xxx</tt>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<profiles.active>test</profiles.active>
<tt>yyy</tt>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
<tt>zzz</tt>
</properties>
</profile>
</profiles>
</project>
yml文件
server:
port: 9088
servlet:
context-path: /test
spring:
profiles:
active: @profiles.active@
application:
name: test
tt: @tt@
@profiles.active@ 是动态传输,因为filtering=true,我们甚至可以在maven打包时传入参数-Pprofile,tt注意是用@包裹起来,由maven传输过去
网友评论