Maven中的<dependencyManagement> 标签

在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> 中。在子模块中,只需要指定依赖的 groupIdartifactId,而无需指定版本号,因为它们会自动继承自父级POM文件。

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值