gpt4 book ai didi

java - Maven 解析依赖项的版本

转载 作者:搜寻专家 更新时间:2023-10-30 21:30:27 25 4
gpt4 key购买 nike

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

例如

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

类似地,在下面详述的场景中,Artifact-A 将使用哪个版本的 Artifact-C?

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

如果您可以提供或链接到有关 maven 如何解析这些版本的简明解释。

最佳答案

参见 Introduction to the Dependency Mechanism :

With transitive dependencies, the graph of included libraries can quickly grow quite large. For this reason, there are some additional features that will limit which dependencies are included:

  • 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.
    • "nearest definition" means that the version used will be the closest one to your project in the tree of dependencies, eg. if dependencies for A, B, and C are defined as A -> B -> C -> D 2.0 and A -> E -> D 1.0, then D 1.0 will be used when building A because the path from A to D through E is shorter. You could explicitly add a dependency to D 2.0 in A to force the use of D 2.0

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

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

然后选择项目B中声明的依赖D。

关于java - Maven 解析依赖项的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30913724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com