在 Maven 项目中,dependencies
和 dependencyManagement
都用于定义项目的依赖关系,但它们的使用场景和功能有所不同。以下是它们的主要区别:
dependencies
- 位置: 位于
pom.xml
文件的<dependencies>
元素内。 - 作用: 直接定义项目的依赖项,表示这些依赖项将被包含在构建中。
- 范围: 这些依赖项会被直接添加到当前项目的类路径中,并且会传递给下游模块(如果当前项目是一个父模块)。
- 例子:
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.10</version> </dependency> <!-- 其他依赖项 --> </dependencies>
dependencyManagement
- 位置: 位于
pom.xml
文件的<dependencyManagement>
元素内,通常在父 POM 或顶级 POM 中。 - 作用: 管理依赖项版本,提供版本控制和统一依赖版本声明,但不直接包含这些依赖项在构建中。
- 范围: 这些依赖项需要在子模块的
<dependencies>
元素中显式声明才能生效。子模块可以引用这些依赖项而不需要指定版本号。 - 例子: