Spring Boot 多环境配置与 Maven Profile + 资源过滤切换指南

发表于 2025-09-19 03:08:29 分类于 默认分类 阅读量 111

Spring Boot 多环境配置与 Maven Profile + 资源过滤切换指南

在 Spring Boot 项目中,经常需要为不同环境(如开发、生产)配置不同的 application.yml。使用 Maven Profile资源过滤,可以在打包时自动替换占位符,实现环境切换,非常方便。


1️⃣ 在 application.yml 中定义占位符

在主配置文件中,使用 Maven 占位符,例如:

spring:
  profiles:
    active: @profiles.active@

🔹 这里的 @profiles.active@ 是占位符,会在 Maven 打包时被替换成对应的环境名称,例如 devprod


2️⃣ 配置 Maven 资源过滤

pom.xml<build> 中添加资源过滤和 Maven 资源插件配置:

<build>
    <!-- 资源过滤配置 -->
    <resources>
        <!-- 1️⃣ 对 application*.yaml 配置文件启用过滤 -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering> <!-- 开启 Maven 变量替换,例如 @profiles.active@ -->
            <includes>
                <include>**/application*.yaml</include> <!-- 只过滤 application.yaml、application-dev.yaml 等文件 -->
            </includes>
        </resource>

        <!-- 2️⃣ 其他资源文件不启用过滤 -->
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <excludes>
                <exclude>**/application*.yaml</exclude> <!-- 排除已经被过滤的 YAML 配置文件 -->
            </excludes>
        </resource>
    </resources>

    <plugins>
        <!-- Spring Boot Maven 插件 -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <!-- Maven 编译插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
        </plugin>

        <!-- Maven 资源插件 - 关键配置 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <delimiters>
                    <!-- 使用 @ 作为分隔符,避免与 Spring 的 ${} 冲突 -->
                    <delimiter>@</delimiter>
                </delimiters>
                <useDefaultDelimiters>false</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>

🔹 说明:

  • 只对 application*.yaml 启用过滤,避免替换其他资源文件。
  • 使用 @ 作为分隔符,避免与 Spring Boot 的 ${} 占位符冲突。

3️⃣ 定义 Maven Profile

pom.xml 中定义不同环境的 profile:

<profiles>
    <!-- 开发环境 -->
    <profile>
        <id>dev</id>
        <properties>
            <!-- 环境标识,需要与配置文件名称对应 -->
            <profiles.active>dev</profiles.active>
            <logging.level>debug</logging.level>
        </properties>
        <activation>
            <!-- 默认环境 -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>

    <!-- 生产环境 -->
    <profile>
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
            <logging.level>warn</logging.level>
        </properties>
    </profile>
</profiles>

🔹 开发环境默认启用,生产环境需要在打包时指定。


4️⃣ 打包与运行

  • 开发环境(默认):
mvn spring-boot:run
  • 生产环境
mvn clean package -D maven.test.skip=true -Pprod

🔹 打包完成后,@profiles.active@ 会被替换成 prod,生成的 jar 文件将使用生产环境配置。


5️⃣ 小结

  • @profiles.active@ 占位符 + Maven Profile + 资源过滤,实现 多环境配置切换
  • 开发环境保持默认 dev 配置,无需额外操作。
  • 打包时指定 -Pprod 即可生成生产环境配置。
  • 可以结合 logging.level、数据库 URL 等其他配置,实现完整环境切换。