SpringBoot Maven Profile配合Spring Profile进行多环境配置和打包


Spring Profile

首先你的 Spring Profile 要有多环境配置文件

invalid image (图片无法加载)

Maven Profile

pom.xml 添加

xml
#识别多环境配置
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
    </profile>
    .....test就不写了
    <profile>
        <id>prod</id>
        <properties>
            <profile.active>prod</profile.active>
        </properties>
    </profile>
</profiles>

此时Maven Profile就配置好了。

invalid image (图片无法加载)

整合 Maven Profile 与 Spring Profile

在对每个环境的打包时,如果不需要把其它环境的配置文件也打包进去,可以进行如下配置:

pom.xml 添加

xml
<build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--先排除所有的配置文件-->
                <excludes>
                    <exclude>application*.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <!--引入所需环境的配置文件-->
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>application-${profile.active}.yml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

修改 application.yml

yaml
spring:
  profiles:
    active: @profile.active@ #此处由maven的环境选择决定

LazzMan 2023年12月8日 22:23 收藏文档