Maven的dependency没有设置version问题记

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问题的链接
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Maven 的 pom.xml 文件中,指定依赖的版本有以下几种写法: 1. 硬编码的版本号:直接指定依赖的版本号,例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>1.0</version> </dependency> ``` 2. 变量:使用变量来指定依赖的版本号,例如: ```xml <properties> <example-library.version>1.0</example-library.version> </properties> <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>${example-library.version}</version> </dependency> ``` 3. Maven 表达式:使用 Maven 的表达式来指定依赖的版本号,例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>[1.0,2.0)</version> </dependency> ``` 这里的 `[1.0,2.0)` 表示依赖的版本号范围是 1.0(包含)到 2.0(不包含)之间的任意版本。 4. 版本范围:使用 Maven 的版本范围来指定依赖的版本号,例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>[1.0,2.0]</version> </dependency> ``` 这里的 `[1.0,2.0]` 表示依赖的版本号范围是 1.0(包含)到 2.0(包含)之间的任意版本。 5. LATEST:使用 Maven 的 LATEST 关键字指定依赖的最新版本,例如: ```xml <dependency> <groupId>com.example</groupId> <artifactId>example-library</artifactId> <version>LATEST</version> </dependency> ``` 这种写法不建议使用,因为 LATEST 关键字只会在本地仓库中查找最新版本,不会检查远程仓库中是否有更新的版本。 总的来说,前三种写法比较常用,建议使用其中的一种来指定依赖的版本号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值