Maven如何解决传递依赖的版本冲突?最近获胜策略 [英] How does Maven resolve version conflicts of transitive dependencies ? nearest-wins strategy

查看:38
本文介绍了Maven如何解决传递依赖的版本冲突?最近获胜策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于习惯了在我的项目中没有任何已使用的未声明或未使用的已声明依赖项.尽管很难跟踪在依赖项中列出的未使用的声明的运行时/测试依赖项:分析...人们只需要在 pom.xml 中向它们写注释或以其他方式管理它们以了解测试或运行时需要它们.

I just finally got used to not having any Used undeclared or Unused declared dependencies in my projects. Although it is very hard to track Unused declared runtime/test dependencies that are listed in dependency:analyze... One just must write comments to them in pom.xml or otherwise manage them to know that they are needed for testing or runtime.

但是解决版本冲突的方法我还不清楚.关于传递依赖.

But the way of resolving version conflict is still unclear to me. Regarding transitive dependencies.

最近获胜策略是如何运作的?什么时候使用一个版本而不是另一个版本?

How does the nearest-wins strategy work exactly? When is one version used over another version ?

  • 如果您使用版本号声明 Used 未声明的依赖项 - 它总是获胜

  • If you declare the Used undeclared dependency with a version number - it always wins

如果没有明确指定依赖版本,Maven 无法解析任何版本关于这种依赖可能出现的冲突(奇怪,但写成 这里)

If one doesn't specify dependency version explicitly, Maven cannot resolve any version conflicts that may arise regarding this dependency (weird, but written here)

如果你没有声明 Undeclared used 依赖,它会从最接近的级别(最近获胜策略)中选择一个传递依赖,如果冲突在同一级别,那么它会以某种方式在版本 A 和版本 B 之间做出决定...也许在处理依赖项时第一个获胜

If you don't declare Undeclared used dependency, it chooses a transitive dependency from the closest level (nearest-wins strategy) and if the conflict is on the same level then it somehow decides between version A over version B ... Maybe the first one it comes to when processing the depenencies wins

推荐答案

我认为依赖解析的工作方式与您描述的完全相同.

I think the dependency resolution works the exact same way you described.

我还认为,如果您将 <scope> 子标签用于您的 <dependency>

I also think your life would be much easier if you use the <scope> child tag to your <dependency>

引用自maven官网:

as quoted from the maven official website:

  1. 编译:这是默认范围,如果未指定则使用.编译依赖项在项目的所有类路径中都可用.此外,这些依赖项会传播到相关项目.
  2. provided:这很像 compile,但表示您希望 JDK 或容器在运行时提供依赖项.例如,在为 Java 企业版构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖设置为提供的范围,因为 Web 容器提供了这些类.此范围仅在编译和测试类路径上可用,并且不可传递.
  3. runtime 这个范围表示依赖不是编译所必需的,而是执行所必需的.它位于运行时和测试类路径中,但不在编译类路径中.
  4. test:这个范围表示应用程序正常使用不需要依赖,只在测试编译和执行阶段可用.
  5. system:这个范围类似于提供的,除了你必须提供明确包含它的 JAR.工件始终可用,不会在存储库中查找.
  6. import:(仅适用于 Maven 2.0.9 或更高版本)此范围仅用于该部分中 pom 类型的依赖项.它指示应将指定的 POM 替换为该 POM 部分中的依赖项.由于它们被替换,具有导入范围的依赖项实际上并不参与限制依赖项的传递性.

这篇关于Maven如何解决传递依赖的版本冲突?最近获胜策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆