Maven 依赖管理(Dependency Management)完全解析(2025 最新版)
一句话总结:
<dependencyManagement> = 统一版本 + 不自动引入,<dependencies> = 实际引入 + 可继承,两者配合 = 版本冲突 0 容忍!
一、核心概念对比
| 特性 | <dependencies> | <dependencyManagement> |
|---|
| 是否自动引入 | Yes 自动引入 | No 仅声明版本 |
| 子模块继承 | No 不继承 | Yes 自动继承版本 |
| 作用范围 | 当前模块 | 父 POM → 所有子模块 |
| 典型场景 | 普通项目 | 多模块 / 企业级 |
graph TD
A[父 POM] --> B[dependencyManagement]
B --> C[子模块 A]
B --> D[子模块 B]
C --> E[dependencies: 无需写 version]
D --> E
二、<dependencyManagement> 实战(多模块神器)
父 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.enterprise</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<spring-boot.version>3.3.5</spring-boot.version>
<lombok.version>1.18.34</lombok.version>
<mysql.version>8.0.33</mysql.version>
</properties>
<!-- 统一版本管理 -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM(推荐) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 自定义版本 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
子模块 pom.xml
<parent>
<groupId>com.enterprise</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>service-user</artifactId>
<dependencies>
<!-- 无需写 version!自动从父继承 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
三、<dependencies> vs <dependencyManagement> 对比示例
| 配置位置 | 子模块是否引入 | 版本来源 |
|---|
父 <dependencies> | Yes 自动引入 | 父版本 |
父 <dependencyManagement> | No 需手动声明 | 父版本 |
子 <dependencies> | Yes | 本地或父 |
最佳实践:父 POM 用 <dependencyManagement>,子模块用 <dependencies>
四、BOM(Bill of Materials)导入
<dependencyManagement>
<dependencies>
<!-- 导入 Spring Boot 所有依赖版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 导入 Spring Cloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
优势:零配置,所有 Spring 组件版本自动对齐!
五、依赖范围(Scope)管理
| Scope | 传递性 | 编译 | 测试 | 运行 |
|---|
compile | Yes | Yes | Yes | Yes |
provided | Yes | Yes | Yes | No |
runtime | Yes | No | Yes | Yes |
test | No | No | Yes | No |
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope> <!-- 容器提供 -->
</dependency>
六、依赖冲突解决(3 大策略)
| 策略 | 说明 | 示例 |
|---|
| 路径最近优先 | 依赖路径最短的版本胜出 | A→B→C(1.0) 胜过 A→D→E→C(2.0) |
| 声明顺序优先 | 同路径,先声明的胜出 | 先写 1.0 → 后写 2.0 → 选 1.0 |
| 手动锁定 | <dependencyManagement> 强制版本 | 统一用 2.0 |
# 查看冲突
mvn dependency:tree -Dverbose
七、依赖分析命令
| 命令 | 作用 |
|---|
mvn dependency:tree | 依赖树 |
mvn dependency:analyze | 未使用 / 重复依赖 |
mvn dependency:purge-local-repository | 清理本地缓存 |
mvn versions:display-dependency-updates | 检查可升级版本 |
八、最佳实践清单
| 实践 | 配置 |
|---|
| 统一版本 | <properties> + <dependencyManagement> |
| 父 POM 管理 | spring-boot-dependencies BOM |
| 禁止 SNAPSHOT | maven-enforcer-plugin |
| 排除冲突 | <exclusions> |
| 定期检查 | mvn versions:display-dependency-updates |
<!-- 禁止 SNAPSHOT -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<goals><goal>enforce</goal></goals>
<configuration>
<rules>
<requireReleaseDeps>
<message>No Snapshots Allowed!</message>
</requireReleaseDeps>
</rules>
</configuration>
</execution>
</executions>
</plugin>
九、完整示例:企业级 parent/pom.xml
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 自定义组件 -->
<dependency>
<groupId>com.company</groupId>
<artifactId>common-utils</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
十、一句话总结
“父 POM 管版本,子模块管引入,BOM 管生态,enforcer 管规范”
恭喜!你已掌握 Maven 依赖管理!
下一步推荐
| 方向 | 内容 |
|---|
| Maven Release 插件 | 自动版本升级 |
| Nexus 版本策略 | SNAPSHOT vs RELEASE |
| IDEA 依赖分析 | Maven Helper 插件 |
需要 完整企业级 parent POM 模板?
回复 Enterprise Parent 立即获取 GitHub 链接!