1 pom.xml
<project>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>pre</id>
<properties>
<spring.profiles.active>pre</spring.profiles.active>
</properties>
</profile>
<profile>
<id>rc</id>
<properties>
<spring.profiles.active>rc</spring.profiles.active>
</properties>
</profile>
</profiles>
</project>
2 配置文件
2.1 自动指定环境
2.1.0 使用双引号
打包时,添加-P参数,指定环境的配置文件,用于Jekins自动化部署。
- application.yml
spring:
application:
name: user-system
profiles:
active: "@spring.profiles.active@"
2.1.2 不使用双引号
- 异常
idea Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token found character ‘@’ that cannot start any token - 方案:添加依赖
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.26</version>
</dependency>
- application.yml
spring:
application:
name: user-system
profiles:
active: @spring.profiles.active@
2.2 环境配置文件
# 开发环境
application-dev.yml
# 测试环境
application-test.yml
# 预发布环境
application-pre.yml
# 发行环境
application-rc.yml
2.3 运行
2.3.1 命令运行
# 指定环境
mvn spring-boot:run -Dspring-boot.run.profiles=dev
- 打包
mvn clean package -P dev
- 运行
# 指定环境
java -jar -Dspring.profiles.active=dev ***.jar
2.3.2 自动运行
2.2 手动指定环境
可以配置多环境文件,打包时,指定特定的环境,不适合自动部署,适用于开发调试。
- application.yml
spring:
application:
name: user-system
profiles:
active: dev
- 环境配置文件:
# 开发环境
application-dev.yml
# 测试环境
application-test.yml
# 预发布环境
application-pre.yml
# 发行环境
application-rc.yml
- 运行
java -jar **.jar
【参考文献】
[1]https://www.cnblogs.com/zhwanwan/p/11898524.html
[2]https://www.cnblogs.com/lizm166/p/10749023.html
[3]https://www.jianshu.com/p/bb8ac8abddc3