跳到主要内容

Profile 配置

在实际开发中,项目通常需要在不同的环境(开发、测试、生产)中运行,不同环境的配置可能不同。Maven 的 Profile 机制可以帮助我们管理多环境配置。

什么是 Profile?

Profile 是 Maven 提供的一种机制,用于在不同环境下使用不同的配置。通过 Profile,可以:

  • 使用不同的依赖版本
  • 使用不同的配置文件
  • 使用不同的构建配置
  • 使用不同的资源文件

Profile 的定义位置

Profile 可以定义在三个位置:

位置作用范围文件
pom.xml当前项目项目级配置
用户 settings.xml当前用户用户级配置
全局 settings.xml所有用户全局配置

在 pom.xml 中定义

<project>
...
<profiles>
<profile>
<id>dev</id>
<properties>
<env>development</env>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
</properties>
</profile>

<profile>
<id>prod</id>
<properties>
<env>production</env>
<db.url>jdbc:mysql://prod-server:3306/prod_db</db.url>
</properties>
</profile>
</profiles>
</project>

在 settings.xml 中定义

<settings>
...
<profiles>
<profile>
<id>my-profile</id>
<properties>
<custom.property>value</custom.property>
</properties>
</profile>
</profiles>

<!-- 默认激活的 Profile -->
<activeProfiles>
<activeProfile>my-profile</activeProfile>
</activeProfiles>
</settings>

Profile 的激活方式

1. 命令行激活

使用 -P 参数激活指定的 Profile:

# 激活单个 Profile
mvn package -Pdev

# 激活多个 Profile
mvn package -Pdev,test

# 激活所有 Profile
mvn package -Pall

2. 默认激活

在 profile 中配置 activeByDefault

<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>development</env>
</properties>
</profile>
</profiles>

3. 根据环境变量激活

<profile>
<id>prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
</profile>

激活方式:

mvn package -Denv=prod

4. 根据 JDK 版本激活

<profile>
<id>jdk-17</id>
<activation>
<jdk>17</jdk>
</activation>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</profile>

5. 根据操作系统激活

<profile>
<id>windows</id>
<activation>
<os>
<family>Windows</family>
</os>
</activation>
</profile>

6. 根据文件存在激活

<profile>
<id>config-exists</id>
<activation>
<file>
<exists>src/main/resources/config.properties</exists>
</file>
</activation>
</profile>

Profile 可配置的元素

Profile 中可以配置以下元素:

<profile>
<id>my-profile</id>

<!-- 激活条件 -->
<activation>
<activeByDefault>false</activeByDefault>
</activation>

<!-- 属性 -->
<properties>
<custom.property>value</custom.property>
</properties>

<!-- 依赖 -->
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>test-lib</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<!-- 依赖管理 -->
<dependencyManagement>
<dependencies>
...
</dependencies>
</dependencyManagement>

<!-- 构建配置 -->
<build>
<plugins>
...
</plugins>
</build>

<!-- 仓库配置 -->
<repositories>
<repository>
<id>special-repo</id>
<url>https://repo.example.com/maven2</url>
</repository>
</repositories>
</profile>

多环境配置实践

项目结构

src/main/resources/
├── application.properties
├── application-dev.properties
├── application-test.properties
└── application-prod.properties

配置 Profile

<project>
...

<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
<db.url>jdbc:mysql://localhost:3306/dev_db</db.url>
<db.username>dev_user</db.username>
<db.password>dev_pass</db.password>
</properties>
</profile>

<!-- 测试环境 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
<db.url>jdbc:mysql://test-server:3306/test_db</db.url>
<db.username>test_user</db.username>
<db.password>test_pass</db.password>
</properties>
</profile>

<!-- 生产环境 -->
<profile>
<id>prod</id>
<properties>
<env>prod</env>
<db.url>jdbc:mysql://prod-server:3306/prod_db</db.url>
<db.username>prod_user</db.username>
<db.password>prod_pass</db.password>
</properties>
</profile>
</profiles>

<build>
<!-- 资源过滤 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
</resources>
</build>
</project>

资源文件中使用变量

application.properties

# 数据库配置
db.url=${db.url}
db.username=${db.username}
db.password=${db.password}

# 环境标识
environment=${env}

构建命令

# 开发环境构建(默认)
mvn package

# 测试环境构建
mvn package -Ptest

# 生产环境构建
mvn package -Pprod

资源过滤

资源过滤允许在资源文件中使用 Maven 属性,构建时自动替换。

启用资源过滤

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>

使用变量

在资源文件中使用 ${...} 语法引用属性:

config.properties

app.name=${project.name}
app.version=${project.version}
build.time=${maven.build.timestamp}
db.url=${db.url}

自定义时间格式

<properties>
<maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

Profile 与 Spring Boot 集成

Spring Boot 有自己的 Profile 机制,可以与 Maven Profile 配合使用。

Maven Profile 与 Spring Boot Profile 映射

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

<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>

application.yml 配置

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

注意:使用 @...@ 语法是因为 Spring Boot 的资源过滤默认使用 @ 分隔符。

配置资源过滤

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

查看 Profile 状态

查看所有 Profile

mvn help:all-profiles

输出示例:

[INFO] Profile Id: dev (Active: true , Source: pom)
[INFO] Profile Id: test (Active: false , Source: pom)
[INFO] Profile Id: prod (Active: false , Source: pom)

查看激活的 Profile

mvn help:active-profiles

查看有效 POM

mvn help:effective-pom

这会显示包含 Profile 配置后的完整 POM。

Profile 最佳实践

1. 合理命名

使用清晰的环境名称:

<profile>
<id>dev</id> <!-- 开发环境 -->
</profile>
<profile>
<id>test</id> <!-- 测试环境 -->
</profile>
<profile>
<id>prod</id> <!-- 生产环境 -->
</profile>

2. 设置默认 Profile

为开发环境设置默认激活,方便日常开发:

<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>

3. 敏感信息管理

不要在 pom.xml 中硬编码敏感信息:

<!-- 不推荐 -->
<properties>
<db.password>my_secret_password</db.password>
</properties>

<!-- 推荐:使用环境变量 -->
<properties>
<db.password>${env.DB_PASSWORD}</db.password>
</properties>

4. 避免过度使用

Profile 不应滥用,只在真正需要不同配置时使用。如果配置差异很小,考虑其他方案。

5. 文档说明

为每个 Profile 添加说明:

<profile>
<id>prod</id>
<properties>
<env>production</env>
</properties>
</profile>

常见问题

问题一:Profile 不生效

原因:可能被其他 Profile 覆盖或未正确激活

解决

  1. 使用 mvn help:active-profiles 检查激活状态
  2. 检查是否有多个 Profile 设置了 activeByDefault
  3. 确保命令行参数正确

问题二:资源过滤不生效

原因:未启用资源过滤或分隔符不正确

解决

  1. 检查 <filtering>true</filtering> 是否配置
  2. Spring Boot 项目使用 @...@ 语法
  3. 普通项目使用 ${...} 语法

问题三:属性值未替换

原因:属性未定义或 Profile 未激活

解决

  1. 检查属性是否在 Profile 中定义
  2. 确保激活了正确的 Profile
  3. 使用 mvn help:effective-pom 检查最终配置

小结

本章我们学习了:

  1. Profile 的概念和作用
  2. Profile 的定义位置和激活方式
  3. 多环境配置的实践方法
  4. 资源过滤的使用
  5. Profile 与 Spring Boot 的集成
  6. Profile 的最佳实践

在下一章中,我们将学习 Maven 仓库与私服的配置。