Maven 如何解决传递依赖的版本冲突?最近胜策略

发布于 2024-11-14 06:58:45 字数 588 浏览 3 评论 0原文

我终于习惯了在我的项目中没有任何已使用的未声明或未使用的已声明依赖项。尽管跟踪 dependency:analyze... 中列出的未使用的已声明运行时/测试依赖项非常困难,但只需在 pom.xml 中对它们编写注释,或者以其他方式管理它们以了解测试或运行时需要它们。

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

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

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

  • 如果没有显式指定依赖项版本, Maven 无法解析任何版本 关于此依赖关系可能出现的冲突(很奇怪,但写成 here)

  • 如果您不声明未声明的使用依赖项,它会从最接近的级别(最近获胜策略)中选择一个传递依赖项,如果冲突位于同一级别,那么它会以某种方式决定在版本 A 与版本 B 之间...也许处理依赖关系时遇到的第一个获胜

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 ?

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

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

  • 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾忌 2024-11-21 06:58:45

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

我还认为,如果您使用 子标签到您的

(引用自 maven 官方网站:

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

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

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

as quoted from the maven official website:

  1. compile:
    This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  2. provided: This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  3. runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  4. test: This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.
  5. system: This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
  6. import: (only available in Maven 2.0.9 or later)
    This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
执手闯天涯 2024-11-21 06:58:45

有一些依赖管理文档的链接:

http://maven .apache.org/guides/introduction/introduction-to-dependency-mechanism.html
(“依赖范围”部分中的导入表)

德国期刊中有一篇神文描述了依赖关系的解决 - 这里是文章的 bibtex 参考: http://www.bibsonomy.org/bibtex/2ef10bb1bc1be7806bc3fba53417bbd5f/funthomas424242

sonatype 书中有一节关于依赖插件的部分:
http://www.sonatype.com/books /mvnex-book/reference/optimizing-sect-dependency-plugin.html

我希望它有帮助。

There are some links to documentation of dependency management:

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
(importend table in section "dependency scope")

There are a god article in a german journal which describe the solving of dependencies - here are the bibtex ref of article: http://www.bibsonomy.org/bibtex/2ef10bb1bc1be7806bc3fba53417bbd5f/funthomas424242

There are a section about the dependency plugin in the sonatype book at:
http://www.sonatype.com/books/mvnex-book/reference/optimizing-sect-dependency-plugin.html

I hope it was helpfull.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文