这是一个非常核心且重要的 Maven 概念。简单来说,它们的关系和区别如下:
一句话总结:dependencyManagement
是"版本管理器",dependencies
是"实际使用者",而 dependency
是最小的"依赖单元"。
下面我们来详细拆解:
1. <dependency>
- 最小依赖单元
这是最基础的依赖声明。它代表一个具体的库(如 spring-boot-starter-web
),包含 groupId
, artifactId
和 version
三要素。
- 作用 :直接将一个库引入到当前项目的 classpath 中,使其可以在代码中被使用。
- 位置 :位于
<dependencies>
标签内部。 - 特点:只要声明了,Maven 就会下载它(以及它的依赖)并打包到最终产物中。
xml
<!-- 这就是一个 dependency -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
2. <dependencies>
- 依赖列表
这是一个容器 ,用来包裹一个或多个 <dependency>
标签。
- 作用 :声明当前项目直接需要的所有依赖。
- 位置 :直接位于
pom.xml
的根元素<project>
下。 - 特点:它是一个"需求清单",清单里的每个依赖都会被实际引入。
xml
<!-- 这是 dependencies 列表 -->
<dependencies>
<!-- 列表里包含多个 dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
3. <dependencyManagement>
- 依赖版本管理中心
这是一个特殊的"版本管理"区域。它本身不会引入任何依赖,只负责声明依赖的版本号、scope 等信息。
- 作用 :为项目(和其子模块)提供统一的依赖版本管理。
- 位置 :位于
pom.xml
的根元素<project>
下,与<dependencies>
同级。 - 特点 :
- 只声明,不引入 :
<dependencyManagement>
里的<dependency>
不会被自动下载,它只是一个"版本仓库"。 - 版本仲裁 :当子模块或当前项目在
<dependencies>
中引入一个依赖时,如果没有指定版本,Maven 会自动到<dependencyManagement>
中查找对应的版本并使用。 - 继承性 :父项目
dependencyManagement
中声明的版本,会被子项目继承。这是 Spring Boot/Cloud 项目中版本统一的关键。
- 只声明,不引入 :
xml
<!-- 这是 dependencyManagement -->
<dependencyManagement>
<dependencies>
<!-- 这里声明的依赖不会被直接引入 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
</dependencyManagement>
核心区别对比
特性 | <dependency> |
<dependencies> |
<dependencyManagement> |
---|---|---|---|
角色 | 依赖单元 | 依赖列表/容器 | 版本管理中心 |
作用 | 将库实际引入项目 | 声明项目的直接依赖 | 统一管理依赖版本 |
是否引入库 | 是 | 是 (引入其内部所有依赖) | 否 (仅声明版本) |
版本号 | 通常需要指定 | 不适用 (它是容器) | 必须指定 (作为版本源) |
典型场景 | 声明单个具体依赖 | 列出项目所有需要的依赖 | 多模块项目统一版本、引入 BOM |
实际应用举例 (Spring Boot 项目)
-
父 POM (
pom.xml
):xml<project> <dependencyManagement> <dependencies> <!-- 引入 Spring Boot BOM,它本身就是一个巨大的 dependencyManagement --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
-
子模块 POM (
pom.xml
):xml<project> <parent> <groupId>com.example</groupId> <artifactId>my-parent-project</artifactId> <version>1.0.0</version> </parent> <dependencies> <!-- 不需要指定 version!版本会从父 POM 的 dependencyManagement 继承 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies> </project>
通过这种方式,整个项目的依赖版本被集中管理,极大地简化了配置并避免了版本冲突。