一、序言
在SpringBoot工程里面可以通过application-*.yml的方式实现多环境打包。但是打包的时候会把所有的.yml配置文件都打到包里。
本篇简介下,通过maven来控制哪些配置需要打包。
二、修改配置文件
1、修改pro文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hong</groupId>
<artifactId>springboot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
.....
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<!--使用${environment}获取值-->
<environment>dev</environment>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
<build>
<finalName>springboot-test-${environment}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<!--排除环境配置文件-->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application-*.yml</exclude>
<exclude>application.yml</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<!-- 打包时包含的文件 -->
<includes>
<!--suppress UnresolvedMavenProperty -->
<include>application-${environment}.yml</include>
<include>application.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
2、修改application.yml文件
spring:
profiles:
active: @environment@
三、配置完成后如何启动
四、maven根据环境变量来打包
- 打赏
请选择打赏方式
- 微信
- 支付宝