Spring Profile
首先你的 Spring Profile 要有多环境配置文件
Maven Profile
在 pom.xml
添加
#识别多环境配置
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
.....test就不写了
<profile>
<id>prod</id>
<properties>
<profile.active>prod</profile.active>
</properties>
</profile>
</profiles>
此时Maven Profile就配置好了。
整合 Maven Profile 与 Spring Profile
在对每个环境的打包时,如果不需要把其它环境的配置文件也打包进去,可以进行如下配置:
在 pom.xml
添加
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<!--先排除所有的配置文件-->
<excludes>
<exclude>application*.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!--引入所需环境的配置文件-->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profile.active}.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
修改 application.yml
spring:
profiles:
active: @profile.active@ #此处由maven的环境选择决定