Maven选择不一致版本的间接依赖: why,以及如何防止?

标签 maven dependency-management

我们遇到了 Maven 选择了不一致版本的间接依赖项的情况,我想了解原因,以及将来如何防止这种情况。

我们的 pom.xml 文件具有以下依赖项:

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>1.1.0.RELEASE</version>
</dependency> 
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>3.1.2.RELEASE</version>
</dependency>

但不直接依赖于 spring-core。

两者都依赖于 spring-core:spring-web 3.1.2.RELEASE 依赖于 spring-core 3.1.2.RELEASE(见 its pom-file),而 spring-data-jpa 1.1.0.RELEASE 依赖于任意 3.x spring-core 版本(准确地说是 [3.0.7.RELEASE,4.0.0.RELEASE),见 its pom-file )。

结合两者,我希望 Maven 选择 spring-core 版本 3.1.2.RELEASE。然而,事实并非如此。相反,它选择范围 [3.0.7.RELEASE,4.0.0.RELEASE) 中的最高值,目前为 3.2.0.RELEASE。

(重现场景:将 the above pom.xml file (gist) 放在它自己的目录中,然后运行 ​​mvn dependency:tree -Dverbose=true :对我来说结果是 this tree (gist)。我在 Linux 和 Maven 3.0 上的 Maven 2.2.1 得到相同的结果.4 在 Windows 上。)

这似乎是错误的,因为它不一致:使用了 spring-web 的 pom-file 不允许的 spring-core 版本。

(当 spring-core 3.2.0.RC1 可用时,我们发生了这种情况:在下一次更新时它突然被选中,我们很幸运,由于 spring-core 3.1 和 3.2 之间的不兼容更改,我们遇到了构建错误。但是下次我们可能不会那么幸运,并且有很难追踪的运行时错误。)

: 我只注意到<dependency>的顺序声明很重要:如果我将 spring-web 放在首位,则选择 spring-core 3.1.2.RELEASE。是什么赋予了?

问题:我们如何让 Maven 选择一致版本的间接依赖项,或者至少在它做出的选择与 pom 文件中指定的版本不符时发出警告?

更新 : 我在这里要求一个通用的解决方案。对于这种特定情况,我知道可以通过在 <dependencyManagement> 中添加依赖项来获得正确的行为。 ,指定我总是想要 spring-core 3.1.2.RELEASE。但是,我希望 Maven 在没有此类特定声明的情况下做正确的事情 (TM)。

最佳答案

您所期望的似乎是合乎逻辑的,但 Maven 没有机会做到这一点。它只是在不知道 spring-data-jpa 的情况下解决依赖关系可能与spring-core有关.

依赖解析的工作原理如描述 here并且是您已经描述的方式:

Dependency mediation - this determines what version of a dependency will be used when multiple versions of an artifact are encountered. Currently, Maven 2.0 only supports using the "nearest definition" which means that it will use 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, until Maven 2.0.8 it was not defined which one would win, but since Maven 2.0.9 it's the order in the declaration that counts: the first declaration wins.



所以我认为防止这种情况的唯一方法是意识到它并在你自己的 pom.xml 中明确设置你需要的版本。

顺便说一句,你为什么认为“这似乎是错误的,因为它不一致:使用了 spring-web 的 pom 文件不允许的 spring-core 版本。”?
<version>x.y</version>的版本说明只是使用此版本的建议(参见“注释”here)。如果您打算强制使用此版本,则必须设置 <version>[x.y]</version> .

关于Maven选择不一致版本的间接依赖: why,以及如何防止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495693/

相关文章:

spring-boot - Spring Boot/Gradle/Querydsl项目有相同的依赖依赖不同版本的另一个依赖

php - 为什么使用 "composer create-project"创建项目后,依赖项不是最新的可用版本?

maven - Gradle 相当于 Maven 程序集

java - 是否可以使用maven-bundle-plugin(bnd)生成plugin.xml

code-generation - Maven - 如何让 Castor 插件在生成源阶段触发?

java - 来自 Maven 依赖项的 Spring Boot RestController 不起作用

java - Maven-dependency-plugin 复制包到几个地方

java - Intellij 不会使用加载时编织进行单元测试吗?

dependency-management - Cargo 的项目特定覆盖

maven - 与Maven的 “copy-dependencies”等效的Gradle吗?