【问题标题】:Maven transitive dependecy breaks buildMaven 传递依赖中断构建
【发布时间】:2012-12-20 15:07:44
【问题描述】:

我有以下问题 - 我试图在我的项目中使用apache commons-lang version 2.6(在 pom.xml 中定义),但由于传递依赖,maven 总是添加版本 3.2.1,这会破坏我的构建。 这是 pom.xml 的相关部分

<properties>
   <commons-lang.version>2.6</commons-lang.version>
</properties>
<dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>${commons-lang.version}</version>
</dependency>

当我尝试运行 mvn dependency:tree 时,我可以看到原因是

的传递依赖
+- org.seleniumhq.selenium:selenium-api:jar:2.26.0:compile (version managed from 2.26.0)
    +- net.sourceforge.htmlunit:htmlunit:jar:2.10:compile
       +- org.apache.commons:commons-lang3:jar:3.1:compile

所以我可以看到问题,但我真的不知道如何解决它。感谢您的任何建议:-)

【问题讨论】:

    标签: java maven build


    【解决方案1】:

    考虑在导致问题的依赖项中使用排除标记。排除标记告诉 maven,您不希望 maven 引入间接依赖,并且仅在这种情况下使用。

    例如,如果我的一个依赖项 A 引入了 B 的 1.0 版本,但我想使用 B 的 2.0 版本,我可以这样做:

    <dependency>
        <groupId>org.mycorp</groupId>
        <artifactID>A</artifactID>
        <version>4.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.mycorp</groupId>
                <artifactId>B</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.mycorp</groupId>
        <artifactID>B</artifactID>
        <version>2.0</version>
    </dependency>
    

    排除标记表示在这种情况下不引入间接依赖项(B 1.0)。请注意,您不必提供版本。一旦你告诉 maven 不要自动引入那个版本的 B,你就会跟随一个明确的依赖关系来定义你想要的 B 的版本(在这种情况下是 2.0 版)。

    【讨论】:

    • 相关部分是什么意思(即您到底需要什么)?我会看看排除项,谢谢提示
    【解决方案2】:

    这个特定的依赖应该破坏你的构建,因为 Commons Lang 3 使用与 Lang 2 不同的包命名。你的代码应该使用来自 Lang2 的类,而 HTMLUnit 将使用来自 Lang3 的类,并且两个 JAR 可以共存。

    也许您应该描述您认为这会如何破坏您的构建,并从构建输出中提取相关摘录。

    【讨论】:

      【解决方案3】:

      我的同事忘记在父 pom 的dependencyManagement 中声明版本,就这样。感谢您的建议!

      【讨论】:

        猜你喜欢
        • 2019-04-02
        • 2019-08-03
        • 1970-01-01
        • 2018-07-06
        • 2013-02-17
        • 2019-08-23
        • 2019-10-29
        • 1970-01-01
        相关资源
        最近更新 更多