maven有profiles标签,标识程序的多个环境参数。
1、pom.xml中
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<!--junit测试默认读取文件-->
<!--打包默认包含配置文件-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<spring.profiles.active>uat</spring.profiles.active>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<spring.profiles.active>pre</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prd</id>
<properties>
<spring.profiles.active>prd</spring.profiles.active>
</properties>
</profile>
</profiles>
注意activeByDefault是默认的环境选择。如果打包时没有-P xxx这个参数,则junit测试默认读取文件、打包默认包含配置文件用activeByDefault。
2、application.yml
spring:
profiles:
active: "@spring.profiles.active@"
3、打包后application.yml会自动替换
spring:
profiles:
active: "dev"