SpringBoot-多环境开发兼容问题(Maven与Boot)

推荐先阅读Spring全家桶

Maven中设置多环境属性

<profiles>
<profile>
<id>dev_env</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro_env</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
<profile>
<id>test_env</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>

<build>
<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>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>

SpringBoot中引用Maven属性

spring:
profiles:
active: ${profile.active}
---
spring:
profiles: dev
server:
port: 80
---
spring:
profiles: pro
server:
port: 81
---
spring:
profiles: test
server:
port: 82