Maven 构建配置文件

Maven 构建配置文件(Profiles)完全解析

一句话总结
Profiles 让同一个 pom.xml 在不同环境(开发/测试/生产)下使用不同的配置!


一、什么是 Profile?

  • Profile 是 pom.xml 中的条件化配置块
  • 根据环境激活不同的依赖、插件、属性、仓库等
  • 避免维护多个 pom.xml
<profiles>
  <profile>
    <id>dev</id>     <!-- 环境名称 -->
    ...
  </profile>
  <profile>
    <id>prod</id>
    ...
  </profile>
</profiles>

二、Profile 激活方式(5 种)

方式说明示例
命令行最常用mvn package -Pdev
settings.xml全局激活<activeProfiles><activeProfile>dev</activeProfile></activeProfiles>
操作系统根据 OS<activation><os><family>windows</family></os></activation>
JDK 版本指定 Java 版本<activation><jdk>17</jdk></activation>
文件存在文件存在则激活<activation><file><exists>src/main/resources/dev.properties</exists></file></activation>

三、Profile 实战:多环境配置(最经典案例)

场景:开发、测试、生产数据库不同

1. 项目结构

src/
├── main/
│   └── resources/
│       ├── application.yml          # 公共配置
│       ├── application-dev.yml      # 开发
│       ├── application-test.yml     # 测试
│       └── application-prod.yml     # 生产

2. pom.xml 配置 Profile

<profiles>
  <!-- 开发环境 -->
  <profile>
    <id>dev</id>
    <activation>
      <activeByDefault>true</activeByDefault> <!-- 默认激活 -->
    </activation>
    <properties>
      <spring.profiles.active>dev</spring.profiles.active>
      <db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
    </properties>
    <build>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <includes>
            <include>**/*.yml</include>
            <include>**/*.xml</include>
          </includes>
          <filtering>true</filtering> <!-- 启用变量替换 -->
        </resource>
      </resources>
    </build>
  </profile>

  <!-- 生产环境 -->
  <profile>
    <id>prod</id>
    <properties>
      <spring.profiles.active>prod</spring.profiles.active>
      <db.url>jdbc:mysql://prod-server:3306/prod_db</db.url>
    </properties>
    <build>
      <finalName>myapp-prod</finalName>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <skipTests>true</skipTests> <!-- 生产跳过测试 -->
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

3. 使用 ${} 占位符(filtering=true

application.yml

spring:
  profiles:
    active: @spring.profiles.active@

db:
  url: @db.url@

四、Profile 常用配置项

配置项说明
<properties>环境变量(如端口、数据库)
<dependencies>不同环境依赖不同 JAR
<repositories>使用私有仓库
<pluginRepositories>插件仓库
<build><resources>资源过滤
<build><plugins>不同打包方式

五、命令行激活 Profile

# 激活 dev 环境
mvn package -Pdev

# 激活多个
mvn package -Pdev,test

# 反向激活(不激活 dev)
mvn package -P!dev

六、settings.xml 全局激活(推荐用于团队)

~/.m2/settings.xml

<settings>
  <activeProfiles>
    <activeProfile>dev</activeProfile>
  </activeProfiles>
</settings>

所有项目默认使用 dev 环境!


七、Profile 继承(父子模块)

<!-- 父 POM -->
<profiles>
  <profile>
    <id>common</id>
    <properties>
      <log.level>INFO</log.level>
    </properties>
  </profile>
</profiles>

子模块自动继承!


八、Profile 最佳实践

场景推荐做法
多环境dev / test / prod
CI/CDJenkins 传 -Pprod
本地开发settings.xml 激活 dev
打包跳过测试prod 环境 -DskipTests
不同 JDK<jdk>17</jdk> 自动切换

九、完整示例:Spring Boot 多环境打包

<profiles>
  <profile>
    <id>dev</id>
    <activation><activeByDefault>true</activeByDefault></activation>
    <properties>
      <profile.active>dev</profile.active>
    </properties>
  </profile>

  <profile>
    <id>prod</id>
    <properties>
      <profile.active>prod</profile.active>
    </properties>
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <profiles>
              <profile>${profile.active}</profile>
            </profiles>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

打包命令:

# 开发包
mvn package

# 生产包
mvn package -Pprod

十、常见问题解决

问题解决方案
Profile not active检查 -P 是否拼写正确
@xxx@ 未替换确保 <filtering>true</filtering>
多个 Profile 冲突使用 ! 反向排除
IDE 不识别IDEA → Maven → Profiles → 勾选

十一、一句话总结

“一个 POM,多个环境,-P 切换全搞定!”


十二、快速上手命令

# 创建项目
mvn archetype:generate -DgroupId=com.example -DartifactId=profile-demo -DarchetypeArtifactId=maven-archetype-quickstart

# 添加 Profile 配置到 pom.xml

# 打包开发版
mvn package

# 打包生产版
mvn package -Pprod

恭喜!你已掌握 Maven Profile!


下一步推荐

方向内容
Spring Boot Profileapplication-{profile}.yml
Docker + Mavenfabric8 插件构建镜像
CI/CD 集成Jenkins/GitLab CI 使用 -P

需要 Spring Boot + Profile 完整项目模板
回复 Spring Boot Profile 立即获取!

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注