【问题标题】:How transitive dependency work for second level dependency传递依赖如何用于二级依赖
【发布时间】:2019-11-04 04:25:22
【问题描述】:

我有以下依赖树。

[INFO] +- net.sf.jasperreports:jasperreports:jar:6.5.1:compile
[INFO] |  +- org.eclipse.jdt.core.compiler:ecj:jar:4.4.2:compile
[INFO] |  +- org.codehaus.castor:castor-xml:jar:1.3.3:compile
[INFO] |  |  +- org.codehaus.castor:castor-core:jar:1.3.3:compile
[INFO] |  |  +- commons-lang:commons-lang:jar:2.6:compile

当我在 pom 文件中添加依赖项“commons-lang”作为顶级依赖项时,它会自动删除 common-lang 的传递依赖项,即使没有从 pom 文件中排除它。

[INFO] +- net.sf.jasperreports:jasperreports:jar:6.5.1:compile
[INFO] |  +- org.eclipse.jdt.core.compiler:ecj:jar:4.4.2:compile
[INFO] |  +- org.codehaus.castor:castor-xml:jar:1.3.3:compile
[INFO] |  |  +- org.codehaus.castor:castor-core:jar:1.3.3:compile
[INFO] |  |  +- javax.inject:javax.inject:jar:1:compile

当我们从 jasperreports 中排除 common-lang 时,应该排除它。

【问题讨论】:

  • 这很奇怪,不应该发生。

标签: maven transitive-dependency


【解决方案1】:

Dependency Mediation 是您所说的规则。这是maven管理传递依赖的规则之一。

它会根据 pom 中最接近的定义将 commons-lang 放入依赖树中。

你可以在这里阅读所有规则:
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

所以引用他们:

依赖中介 - 这决定了当遇到多个版本作为依赖时将选择工件的哪个版本。 Maven 选择“最近的定义”。也就是说,它使用依赖关系树中与您的项目最接近的依赖关系的版本。您始终可以通过在项目的 POM 中明确声明来保证版本。

commons-lang 的依赖永远不会被移除,它在传递树中的位置已经改变。现在它已成为一级依赖。

[INFO] +- commons-lang:commons-lang:jar:2.6:compile
[INFO] \- net.sf.jasperreports:jasperreports:jar:6.5.1:compile
[INFO]    +- org.eclipse.jdt.core.compiler:ecj:jar:4.4.2:compile
[INFO]    +- org.codehaus.castor:castor-xml:jar:1.3.3:compile
[INFO]    |  +- org.codehaus.castor:castor-core:jar:1.3.3:compile
[INFO]    |  +- javax.inject:javax.inject:jar:1:compile
[INFO]    |  +- stax:stax:jar:1.2.0:compile
[INFO]    |  |  \- stax:stax-api:jar:1.0.1:compile
[INFO]    |  \- javax.xml.stream:stax-api:jar:1.0-2:compile
[INFO]    +- com.fasterxml.jackson.core:jackson-core:jar:2.1.4:compile

【讨论】:

  • 我们还需要从 jasperreports 中排除 commons-lang 吗?
【解决方案2】:

当你的项目中声明了依赖项时,pom maven 将使用该声明而不是传递依赖项。

省略的依赖项不会显示在 maven 依赖项树中,我刚刚了解到,由于 maven-dependency-plugin 版本 3.0 不再支持详细选项 - 这将显示省略的依赖项。

当我尝试在 maven 命令行中提供 -Dverbose 选项时看到这一点(对于“mvn dependency:tree -Dverbose=true”):

 Verbose not supported since maven-dependency-plugin 3.0

Display omitted versions in maven dependency:tree? 中查看 cmets 的答案

【讨论】:

    猜你喜欢
    • 2021-07-26
    • 2012-10-09
    • 2014-11-28
    • 2018-07-06
    • 2019-10-31
    • 2019-08-23
    • 2019-10-29
    • 2022-11-22
    相关资源
    最近更新 更多