String boot 项 配置文件application.yml,但是为了一个项目可以在开发环境,测试环境,生产环境执行,那么就要配置不通的配置文件,如:application-dev.yml、application-uat.yml、application-prod.yml
环境发布时,需要打包项目,无论jar包 还是war包,可以指定打包环境,即指定特定配置文件
1、application.yml 中配置如下,其中 ‘@spring.profiles.active@’ 就是指定打包配置文件的引用。
@@ 是说明引用pom.xml 配置文件中的配置,网上有些没有单引号,我配置时没有单引号时,yml文件报错,记得单引号一定要,否则可能会报错,如下会说明:
spring.profiles.active 为pom.xml 文件中的配置
spring:
application:
name: xxx-xxx
profiles:
active: '@spring.profiles.active@'
2、pom.xml 配置说明
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<activation><!--默认使用dev的配置,一般用于本地启动时-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>uat</id>
<properties>
<spring.profiles.active>uat</spring.profiles.active>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<spring.profiles.active>pro</spring.profiles.active>
</properties>
</profile>
</profiles>
同时为了Springboot打包时可以获取到pom.xml 中的配置,pom.xml 中配置 <resources>标签
<build>
<finalName>gp-lotso-web</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、打包
mvn clean package -P uat
uat 就是指定打包使用的配置文件,打包完成后的jar包或war包,
配置文件application.yml 中的 环境标签变为如下,即可启动项目了
spring:
application:
name: xxx-xxx
profiles:
active: 'uat'