文章目录
- 1、SpringBoot自带指定环境
- 多profile文件形式
- yml文档块形式
- 激活指定profile的几种方式
- 2、通过POM自定义属性值指定环境
- 3、打包时指定POM中的profile值指定环境
java -jar xxx.jar --server.port=8080
java -jar xxx.jar --server.port=8081
上面的启动命令可以指定配置属性。
1、SpringBoot自带指定环境
多profile文件形式
- 格式:
application-{profile}.properties/yml
,如:application-dev.properties、application-prd.properties等。 - Spring Boot 默认都是从全局配置文件
application.properties
和application.yml
进入开始读取配置。 - 可以使用约定格式
application-{profile}.properties/yml
写任意多的配置文件。 - 然后在全局配置文件
application.properties/yml
激活它们即可,使用spring.profiles.active=dev
application-dev.properties
test.name=vermouth.dev
server.port=8081
application-prd.properties
test.name=vermouth.prd
server.port=8082
yml文档块形式
yml 文件支持多文档块方式,同一个yml文件中,可以使用"—"来区分不同的文档,相当于不同的配置文件。
application.yml
spring:
profiles:
active: prd
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prd #指定属于哪个环境
激活指定profile的几种方式
- 在全局配置文件中指定:
spring.profiles.active=dev
- 命令行:
java -jar spring-boot.jar --spring.profiles.active=dev
- 在IDEA中配置虚拟机参数:
-Dspring.profiles.active=dev
上面的配置会激活application-dev.properties
配置文件。
2、通过POM自定义属性值指定环境
把环境信息配置在不同的文件夹中,在编译打包时根据pom配置加载不同文件夹下的配置文件即可。
1、创建不同环境的配置文件夹及配置文件
2、修改pom文件
<properties>
<env>prd</env>
</properties>
<build>
<!-- 指定打包后的文件名-->
<finalName>${project.artifactId}-${env}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/conf/${env}</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、编译打包
指定evn值为prd,发现只加载了conf/prd文件夹下的配置文件。
同理,如果指定env值为dev,只会加载conf/dev文件夹下的配置文件。
3、打包时指定POM中的profile值指定环境
把环境信息配置在不同的配置文件中,在编译打包时加载不同的配置文件即可。
1、创建多个配置文件
application.properties:
spring.profiles.active=@profileActive@
application-dev.properties:
test.name=vermouth.dev
application-prd.properties:
test.name=sherry.prd
2、修改pom文件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<!-- 根据打包时激活的profile来包括对应的配置文件 -->
<include>application.properties</include>
<include>application-${profileActive}.properties</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>dev</id>
<activation>
<!-- 如果未指定环境,默认dev-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profileActive>dev</profileActive>
</properties>
</profile>
<profile>
<id>prd</id>
<properties>
<profileActive>prd</profileActive>
</properties>
</profile>
</profiles>
3、打包
命令打包指定环境:
mvn clean package -P prd
IDEA 工具打包指定环境:
如下图可见,打包后就只加载了application.properties和application-prd.properties,且application.properties文件中的spring.profiles.active=prd