【问题标题】:Override dependencies of third party jar in maven覆盖maven中第三方jar的依赖
【发布时间】:2011-06-30 23:17:05
【问题描述】:

像这样的org.carrot2 取决于commons-httpclient 3.1 那么我如何将这个commons-httpclient 3.1 更改为HttpClient 4.1.1。我正在日食中工作。因为我想删除 commons-httpclient:3.1 来自那些依赖这个 jar 文件的人,我想用 HttpClient 4.1.1 替换。

所以我想要做什么.. 我从依赖层次结构文件夹中双击这个 org.carrot2 并进入它的 pom.xml 文件并试图将 commons-httpclient 3.1 更改为 httpclient 4.1.1 但它不允许我更改为退格和删除不起作用..

任何建议将不胜感激..

【问题讨论】:

    标签: java eclipse maven


    【解决方案1】:

    首先请确保上述工件可以与 HttpClient 4.1.1 正常工作

    我们可以为每个依赖项定义“排除”,正如http://maven.apache.org/pom.html#Exclusions中提到的那样

    排除明确告诉 Maven 你不想包括 作为依赖项的指定项目 这种依赖性(换句话说, 它的传递依赖)

    排除项:排除项包含一个或 更多的排除元素,每个 包含 groupId 和 artifactId 表示要排除的依赖项。 与可选的不同,它可能会也可能不会 安装和使用,排除 主动将自己从 依赖树。

    <dependencies>
      <dependency>
        <groupId>the_group</groupId>
        <artifactId>the_artifact</artifactId>
        <version>the_version</version>
        <exclusions>
          <exclusion>
            <groupId>the_apache_group</groupId>
            <artifactId>the_http_client_artifact</artifactId>
          </exclusion>
        </exclusions>
      </dependency>
    
      <dependency>
        <groupId>the_apache_group</groupId>
        <artifactId>the_http_client_artifact</artifactId>
        <version>4.1.1</version>
      </dependency>
      ...
    </dependencies>
    

    我希望这可能有助于实现要求。

    问候,

    查理·Ch.

    【讨论】:

    • Chitsuk,感谢您的回复..&lt;dependencies&gt;&lt;dependency&gt; &lt;groupId&gt;org.apache.solr&lt;/groupId&gt;&lt;artifactId&gt;solr-solrj&lt;/artifactId&gt;&lt;version&gt;3.2.0&lt;/version&gt;&lt;exclusions&gt; &lt;exclusion&gt;&lt;groupId&gt;commons-httpclient&lt;/groupId&gt;&lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt;&lt;/exclusion&gt;&lt;/exclusions&gt;&lt;/dependency&gt; &lt;dependency&gt;&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt; &lt;artifactId&gt;httpclient&lt;/artifactId&gt;&lt;version&gt;4.1.1&lt;/version&gt; &lt;/dependency&gt;&lt;/dependencies&gt; 所以在此我将 commonshttpclient 作为 org.apache.solr 的依赖项删除到 httpclient4.1.1 对吗??
    • 是的,你是对的。将使用 HttpClient 4.1.1。
    • Chitsuk,我的 pom.xml 中已经有了依赖项标签,所以我应该只复制这个 &lt;dependency&gt; &lt;groupId&gt;org.apache.solr&lt;/groupId&gt;&lt;artifactId&gt;solr-solrj&lt;/artifactId&gt;&lt;version&gt;3.2.0&lt;/version&gt;&lt;exclusions&gt; &lt;exclusion&gt;&lt;groupId&gt;commons-httpclient&lt;/groupId&gt;&lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt;&lt;/exclusion&gt;&lt;/exclusions&gt;&lt;/dependency&gt; &lt;dependency&gt;&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt; &lt;artifactId&gt;httpclient&lt;/artifactId&gt;&lt;version&gt;4.1.1&lt;/version&gt; &lt;/dependency&gt; 对吧?
    • Chitsuk,我一保存它就会自动构建一切。还是我必须做点什么……??
    【解决方案2】:

    将对 HttpClient 4.1.1 的依赖添加到 您的 POM。 Maven 将识别您的直接依赖和间接依赖之间的冲突(假设 httpclient 的 groupId 和 artifactId 没有改变),并使用较新的版本。 (不是因为它较新,而是因为它更直接)

    你不能编辑其他人的 pom 文件是有道理的——毕竟,你希望胡萝卜只在你的程序中使用较新的 http 客户端,而不是在所有使用胡萝卜的程序中......

    【讨论】:

    • @meriton,我如何添加对 HttpClient 4.1.1 的依赖项。因为我希望 org.carrot2 依赖于 HttpClient 4.1.1 而不是 commons-httpclient 3.1
    • 无论你的 pom 声明对 httpclient 的依赖还是胡萝卜的依赖都无关紧要,它们最终都在同一个类路径中。而且我相信您知道如何添加依赖项,因为胡萝卜以某种方式最终出现在您的依赖项层次结构中......
    • @meriton,我在做其他一些项目,他离开了这个项目。所以我必须完成这个。我在我的 pom.xml 文件中添加了这个&lt;dependency&gt; &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt; &lt;artifactId&gt;httpclient&lt;/artifactId&gt; &lt;version&gt;4.1.1&lt;/version&gt; &lt;/dependency&gt; 但是那些依赖commons-httpclient 3.1的人仍然依赖这个..
    • 上面的答案有问题 - Maven 最终不会使用最新的依赖项。相反,它将根据最小距离解决两个依赖项之间的冲突,因此它将选择您在自己的 pom 中本地声明的一个,因为它比另一个更接近。
    • @Eugen,感谢您的回复..&lt;dependencies&gt;&lt;dependency&gt; &lt;groupId&gt;org.apache.solr&lt;/groupId&gt;&lt;artifactId&gt;solr-solrj&lt;/artifactId&gt;&lt;version&gt;3.2.0&lt;/version&gt;&lt;exclusions&gt; &lt;exclusion&gt;&lt;groupId&gt;commons-httpclient&lt;/groupId&gt;&lt;artifactId&gt;commons-httpclient&lt;/artifactId&gt;&lt;/exclusion&gt;&lt;/exclusions&gt;&lt;/dependency&gt; &lt;dependency&gt;&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt; &lt;artifactId&gt;httpclient&lt;/artifactId&gt;&lt;version&gt;4.1.1&lt;/version&gt; &lt;/dependency&gt;&lt;/dependencies&gt; 所以在此我将 commonshttpclient 作为 org.apache.solr 的依赖项删除到 httpclient4.1.1 对吗??
    【解决方案3】:

    如果某些东西依赖于 HttpClient 3.x 将无法替代 4.x,因为它们是完全不同的 API。尝试访问依赖于3.x 的代码时会出现运行时错误。

    【讨论】:

    • 希望你记得我。根据你的建议,我开始使用 HttpClient 4.1.1,但我遇到了新错误。 link
    • 这可能会被视为评论,但这不是对 op 问题的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多