Maven解析依赖项的版本

Maven解析依赖项的版本

本文介绍了Maven解析依赖项的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果模块的两个依赖关系都有一个共同的depdnendency但在poms中指定了不同的版本,那么在构建模块时使用哪个版本?

If two dependences of a module both have a common depdnendency but have different versions specified in their poms, which version is used when building the module?

例如

        Artifact-A
        /       \
       /         \
      /           \
Artifact-B      Artifact-C
      \           /
 1.6.0 \         / 1.8.0
        \       /
        Artifact-D

同样在下面详述的场景中,神器-A会使用什么版本的神器-C?

Similarly in the scenario detailed below what version of Artifact-C would Artifact-A use?

    Artifact-A
        |      \
        |       |
        |       |
    Artifact-B  | 1.60.0
        |       |
 1.62.0 |       |
        |      /
    Artifact-C

如果你能提供或链接到maven如何简洁的探索解析这些版本。

If you could provide or link to a concise explination of how maven resolves these versions.

推荐答案

参见:


  • 依赖性调解 - 这确定在遇到多个版本的工件时将使用哪个版本的依赖项。目前,Maven 2.0仅支持使用最近定义,这意味着它将在依赖树中使用与项目最接近的依赖项版本。您可以通过在项目的POM中明确声明它来保证版本。 请注意,如果两个依赖项版本在依赖关系树中处于相同的深度,那么直到Maven 2.0.8没有定义哪一个会赢,但是从Maven 2.0.9开始,它就是声明中的顺序:第一个声明胜利

  • 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.


这意味着对于您的第一个示例(并启动Maven 2.0.9),如果工件B在工件C之前被声明为A中的依赖项,如下所示:

This means that for your first example (and starting Maven 2.0.9) if artifact B is declared as a dependency in A before artifact C as follows:

<dependency>
   <groupId>groupB</groupId>
   <artifactId>projectB</artifactId>
</dependency>
<dependency>
   <groupId>groupC</groupId>
   <artifactId>projectC</artifactId>
</dependency>

然后选择项目B中声明的依赖关系D.

then the dependency D declared in project B is chosen.

这篇关于Maven解析依赖项的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:51