springboot配置maven激活配置文件

在Spring Boot项目中,可以通过Maven配置来激活不同的配置文件(profiles)。以下是几种常见的配置方式:

1. 基础配置文件结构

首先,在 src/main/resources 目录下创建不同环境的配置文件:

复制代码
src/main/resources/
├── application.properties          # 主配置文件
├── application-dev.properties      # 开发环境
├── application-test.properties     # 测试环境
└── application-prod.properties     # 生产环境

2. Maven Profile配置

pom.xml 中配置Maven profiles:

xml 复制代码
<project>
    <!-- 其他配置 -->
    
    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <!-- 默认激活开发环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
        
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
            </properties>
        </profile>
    </profiles>
</project>

3. 配置文件过滤

3.1 在 pom.xml 中配置资源过滤:

xml 复制代码
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.yml</include>
            </includes>
        </resource>
    </resources>
    
    <!-- 使用Maven属性替换占位符 -->
    <filters>
        <filter>src/main/resources/application-${activatedProperties}.properties</filter>
    </filters>
</build>

4. 通过Spring Boot配置

4.1 在 application.properties 中指定激活的profile:

properties 复制代码
# 主配置文件
spring.profiles.active=@activatedProperties@

4.2 或者使用YAML格式:

yaml 复制代码
# application.yml
spring:
  profiles:
    active: @activatedProperties@

5. 使用Maven命令激活

5.1 打包时指定profile:

bash 复制代码
# 打包使用开发环境
mvn clean package -Pdev

# 打包使用测试环境
mvn clean package -Ptest

# 打包使用生产环境
mvn clean package -Pprod

5.2 运行应用时指定:

bash 复制代码
# 方式1:通过Maven运行
mvn spring-boot:run -Dspring-boot.run.profiles=dev

# 方式2:使用jar运行时指定
java -jar target/your-app.jar --spring.profiles.active=prod

6. 高级配置示例

6.1 完整 pom.xml 示例:

xml 复制代码
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    
    <properties>
        <java.version>11</java.version>
        <activatedProperties>dev</activatedProperties>
    </properties>
    
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
                <server.port>8080</server.port>
                <datasource.url>jdbc:mysql://localhost:3306/dev_db</datasource.url>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        
        <profile>
            <id>prod</id>
            <properties>
                <activatedProperties>prod</activatedProperties>
                <server.port>80</server.port>
                <datasource.url>jdbc:mysql://prod-server:3306/prod_db</datasource.url>
            </properties>
        </profile>
    </profiles>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
        
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
</project>

7. 多环境YAML配置

7.1 使用YAML多文档格式:

yaml 复制代码
# application.yml
spring:
  profiles:
    active: @activatedProperties@
---
# 开发环境配置
spring:
  config:
    activate:
      on-profile: "dev"
server:
  port: 8080
datasource:
  url: jdbc:mysql://localhost:3306/dev_db
---
# 生产环境配置
spring:
  config:
    activate:
      on-profile: "prod"
server:
  port: 80
datasource:
  url: jdbc:mysql://prod-server:3306/prod_db

8. IDE中激活Profile

8.1 IntelliJ IDEA:

  • 打开Maven工具栏
  • 选择需要的Profile
  • 或者编辑运行配置,在VM Options中添加:-Dspring.profiles.active=dev

8.2 Eclipse:

  • 右键项目 → Maven → Select Maven Profiles
  • 选择需要激活的Profile

9. 最佳实践建议

  1. 分离配置:将不同环境的配置完全分离
  2. 敏感信息:生产环境密码等敏感信息不要放在代码库中
  3. 环境变量:考虑使用环境变量覆盖配置
  4. 配置优先级:了解Spring Boot配置加载顺序

这样配置后,你可以通过不同的Maven Profile来构建和运行不同环境的Spring Boot应用。

相关推荐
mldong4 小时前
C# 开发者也有自己的轻量工作流引擎了:NuGet 装包,5 分钟跑通一条审批流
后端·c#·.net
小小猪的春天5 小时前
Java 手写第一个 MCP Server:Spring AI MCP 半小时跑通
java·人工智能·spring boot·ai编程
爱学堂IT分享6 小时前
鹤翔老师pmp项目管理培训认证考试课 (直播录播题库送学时证明)【共58课时】-51CTO学堂
后端
王的宝库6 小时前
Go 项目结构:从单文件到标准工程布局
开发语言·后端·golang
码事漫谈7 小时前
类型即世界:一个 C++ 程序员的本体论入门
后端
Rain的Java大神之路7 小时前
JavaWeb开发如何解决跨域问题
java·前端·后端·nginx·web安全·面试·运维开发
计算机毕设定制辅导-无忧学长8 小时前
《基于SpringBoot青年公寓出租管理系统的设计与实现》
java·vue.js·spring boot·青年公寓出租管理系统
泡海椒9 小时前
JQuick-Curl 二次开发:自定义解析器、扩展点开发指南,如何把框架能力真正变成团队资产
java·开发语言·okhttp·maven
MetaLite9 小时前
AI编程与工程底座-让AI遵守SpringBoot边界
人工智能·spring boot·ai编程
IT_陈寒9 小时前
Python的多线程居然是个假把式?搞清GIL让我少熬三天夜
前端·人工智能·后端