Maven的dependency没有设置version问题记

本文探讨了Maven项目中依赖版本未明确指定时的解析机制,通过使用mvn help:effective-pom命令揭示了依赖的实际来源,特别是对于Bill of Materials(BOM)的版本解析过程。

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

Maven的dependency没有设置version问题记

今天一个同事问了在工程里没有看到某个dependency设置version的话,是如何确定的版本呢?

<dependency>
	<groupId>org.elasticsearch.client</groupId>
	<artifactId>transport</artifactId>
</dependency>

查了一下SO,主要说的来自dependencyManagement和transparent dependency。

使用mvn dependency:tree -DskipTests > a.txt输入文件,也没有发现哪里定义了版本。

然后发现了一个命令mvn help:effective-pom可以查看每个module具体生效的pom。

The effective-pom goal is used to make visible the POM that results from the application of interpolation, inheritance and active profiles. It provides a useful way of removing the guesswork about just what ends up in the POM that Maven uses to build your project. It will iterate over all projects in the current build session, printing the effective POM for each.

内容大概是下面的格式:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>xxx.xxx.xxx</groupId>
    <artifactId>xxx-xxx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <modules>
      <module>xxx-xx-xx</module>
    </modules>
    <properties>
      <maven.compiler.encoding>utf-8</maven.compiler.encoding>
    </properties>
    <dependencyManagement>
      <dependencies>
        ...
      </dependencies>
    </dependencyManagement>
    <dependencies>
      ...
    </dependencies>
    <repositories>
      ...
    </repositories>
    <pluginRepositories>
      ....
    </pluginRepositories>
    <build>
      ...
    </build>
    <reporting>
      ...
    </reporting>
    <profiles>
      ...
    </profiles>
  </project>

可以再加上-Dverbose=true, 输出的更直观。

<dependency>
          <groupId>org.elasticsearch.client</groupId>  <!-- xxxx:xxx-product-spring-boot-dependencies:1.4.7-SNAPSHOT, line 58 -->
          <artifactId>transport</artifactId>  <!-- xxxx:xxx-product-spring-boot-dependencies:1.4.7-SNAPSHOT, line 59 -->
          <version>5.2.2</version>  <!-- xxxx:xxx-product-spring-boot-dependencies:1.4.7-SNAPSHOT, line 60 -->

2024更新

以为掌握了这个手段就无所不可了,但没想到今天又遇到这个问题,并且发现很难解决。

本次的情况更为特殊。

A
  --CA
  --CB
  --CC
  --CD

以上面的工程结构为例. A继承了SpringBoot的2.4.0的parent,然后在CC中引用了

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

按照SpringBoot的2.4.0版本,会引入如下的依赖

spring-boot-starter-data-elasticsearch:jar:2.4.0
	spring-data-elasticsearch:jar:4.1.1

但实际情况却是spring-data-elasticsearch:jar:4.2.5,导致SpringBoot启动失败。

为什么会引入这个版本呢?首先尝试了mvn help:effective-pom -Dverbose命令,输出以下结果:

...
 <dependency>
   <groupId>org.springframework.data</groupId>  <!-- org.springframework.data:spring-data-bom:2021.0.5, line 77 -->
   <artifactId>spring-data-elasticsearch</artifactId>  <!-- org.springframework.data:spring-data-bom:2021.0.5, line 78 -->
   <version>4.2.5</version>  <!-- org.springframework.data:spring-data-bom:2021.0.5, line 79 -->
 </dependency>
 ...

但在SpringBoot 2.4.0的depencies中引入的却是:

<spring-data-bom.version>2020.0.1</spring-data-bom.version>
...
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-bom</artifactId>
  <version>${spring-data-bom.version}</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>

值得注意的是在effective-pom中并没有能够判断这种bom是来自于哪个dependencyManagement的依赖。
所以本次就只能手动去翻了A工程的dependencyManagement部分,果然发现有Spring Boot版本上的冲突。

于是就记录在这里。希望有一天能够找到bom来自于哪的命令,这样就不用再手动去翻找了。

附录
  1. SO问题的链接

开发板推荐:天空星STM32F407VET6开发板

超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值