MAVEN依赖的版本仲裁机制

本文详细介绍了Maven的依赖版本仲裁规则,包括最短路径优先、相同路径优先声明及显式指定版本优先原则。Spring Boot项目通过父项目`spring-boot-starter-parent`和`spring-boot-dependencies`实现自动版本仲裁,允许子项目引用依赖时无需指定版本。若需指定版本,可在父项目或子项目中直接声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面

官方文档:Introduction to the Dependency Mechanism

  • Dependency mediation - this determines what version of an artifact will be chosen when multiple versions are encountered as dependencies. Maven picks the “nearest definition”. That is, it uses the version of the closest dependency to your project in the tree of dependencies. You can always guarantee a version by declaring it explicitly in your project’s POM. Note that if two dependency versions are at the same depth in the dependency tree, the first declaration wins.
  • Dependency management - this allows project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified.
规则总结:

MAVEN依赖的版本仲裁机制根据官方文档总结如下(并不是版本越大越优先使用的):
① 最短路径优先原则
② 相同路径优先声明原则
③ 显示指定优先原则

MAVEN版本仲裁

① 最短路径优先原则请添加图片描述
② 相同路径优先声明原则

在这里插入图片描述

③ 显示指定优先原则

如果A组件明确需要使用D的哪个版本,则需要在A组件中明确引用D组件并指定它的版本。

SPRING-BOOT的版本仲裁

每一个SpringBoot工程他都会指定父项目,父项目的主要作用是一般是来做依赖管理,父项目中可以声明非常多的依赖,子项目只要继承了父项目,子项目以后写依赖就不需要指定版本号了。

① 父项目[spring-boot-starter-parent]
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.4.2</version>
</parent>
② 父项目依赖于父依赖[spring-boot-dependencies]

在父依赖中声明了我们日常开发过程中需要用到的所有常用JAR包的版本号和相关依赖,这就是自动版本仲裁机制的基础和前提。

在这里插入图片描述在这里插入图片描述

那么,也就是说:
在我们的项目中,可以使用在spring-boot-dependencies中定义的依赖,而无需指定依赖的版本。
如:子项目B-》父项目A-》spring-boot-starter-parent-》spring-boot-dependencies
由于在spring-boot-dependencies中定义了:

<dependencyManagement>
	...
    <dependencies>
      <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-amqp</artifactId>
        <version>${activemq.version}</version>
      </dependency>
      <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>${antlr2.version}</version>
      </dependency>
      ...
</dependencyManagement>

即使在父项目A中未指定activemq和antlr2的版本,也未进行定义,在子项目B中依然可以进行如下依赖的引入,这就是版本仲裁机制:

<dependencies>
   <dependency>
       <groupId>org.apache.activemq</groupId>
       <artifactId>activemq-amqp</artifactId>
   </dependency>
   <dependency>
       <groupId>antlr</groupId>
       <artifactId>antlr</artifactId>
   </dependency>
   ...
</dependencies>
③ 如果想使用指定的版本怎么办?

A. 只在父项目A的中指定对应的版本即可,这样整个项目就会使用指定的版本。(需要去spring-boot-dependencies中查找该依赖的版本的命名)

<properties>
    <common-pool.version>2.6.2</common-pool.version>
    <lombok.version>1.18.16</lombok.version>
    ...
</properties>

在这里插入图片描述

B. 直接在子项目B中引入依赖并指定版本

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.6.2</version>
</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值