在Maven中,<dependencyManagement>
标签用于集中管理项目中所有模块的依赖版本。通常情况下,它被放置在父级POM文件中,并且不会实际引入任何依赖,而是用来定义依赖的版本号。子模块可以继承这些版本号,简化了在子模块中指定依赖版本的工作。
以下是一个简单的示例,展示了 dependencyManagement
标签的用法
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.0</version>
</dependency>
<!-- 其他依赖声明 -->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 这里不需要指定版本号,它会从dependencyManagement中继承 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<!-- 其他依赖声明 -->
</dependencies>
</project>
在上面的示例中,my-parent
项目定义了 Spring Core 和 Spring Web 的版本号在 <dependencyManagement>
中。在子模块中,只需要指定依赖的 groupId
和 artifactId
,而无需指定版本号,因为它们会自动继承自父级POM文件。