一、一个配置文件:不推荐
# 默认激活dev配置
spring:
profiles:
active: "dev"
---
spring:
config:
activate:
on-profile: "dev"
desc: this is dev environment
---
spring:
config:
activate:
on-profile: "test"
desc: this is test environment
---
spring:
config:
activate:
on-profile: "prod"
desc: this is prod environment
二、不同环境,不同文件
在Spring Boot中多环境配置文件名需要满足application-{profile}.properties的格式
其中{profile}对应你的环境标识,比如:
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
至于哪个具体的配置文件会被加载,需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
如:spring.profiles.active=test就会加载application-test.properties配置文件内容
三、profiles目录
创建src/main/resources/profiles目录
profiles目录下创建dev、prod、test等文件夹,每个文件夹下创建application.yml
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
<build>
<resources>
<resource>
<directory>src/main/profiles/dev</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
<build>
<resources>
<resource>
<directory>src/main/profiles/prod</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
mvn clean package -Pprod -U
# 或者
mvn clean package -DprofileActive=prod -U