eclipse 覆盖maven中第三方jar的依赖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6542118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:32:58  来源:igfitidea点击:

Override dependencies of third party jar in maven

javaeclipsemaven

提问by arsenal

Like this org.carrot2is depending on commons-httpclient 3.1So how I can change this commons-httpclient 3.1to HttpClient 4.1.1. I am working in eclipse. As I want to remove commons-httpclient:3.1from those who are depending on this jar file and I want to replace with HttpClient 4.1.1.

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

So what I was trying to do.. I doubled click on this org.carrot2from dependency hierarchy folder and went into its pom.xml file and was trying to change commons-httpclient 3.1to httpclient 4.1.1 but it is not allowing me to change as backspace and delete is not working on that..

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

Any suggestions will be appreciated..

任何建议将不胜感激..

回答by Charlee Chitsuk

Firstly please ensure that the mentioned artifact can work properly with the HttpClient 4.1.1.

首先请确保提到的工件可以与 HttpClient 4.1.1 一起正常工作

We can define "the exclusion" for each dependency as it is mentioned at http://maven.apache.org/pom.html#Exclusions

我们可以为每个依赖项定义“排除”,因为它在http://maven.apache.org/pom.html#Exclusions 中提到

Exclusions explicitly tell Maven that you don't want to include the specified project that is a dependency of this dependency (in other words, its transitive dependency)

exclusions: Exclusions contain one or more exclusion elements, each containing a groupId and artifactId denoting a dependency to exclude. Unlike optional, which may or may not be installed and used, exclusions actively remove themselves from the dependency tree.

排除明确告诉 Maven 您不想包含作为此依赖项的依赖项的指定项目(换句话说,其传递依赖项)

excludes:排除包含一个或多个排除元素,每个包含一个 groupId 和 artifactId 表示要排除的依赖项。与可能安装或不安装和使用的 optional 不同,排除项会主动从依赖关系树中删除自己。

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

I hope this may help to achieve the requirement.

我希望这可能有助于达到要求。

Regards,

问候,

Charlee Ch.

查理·Ch。

回答by meriton

Add a dependency on HttpClient 4.1.1 to yourPOM. Maven will recognize the conflict (assuming groupId and artifactId of httpclient have not changed) between your direct dependency, and the indirect dependency, and use the newer version. (not because its the newer one, but because it is the more direct one)

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

And it makes sense you can't edit other people's pom files - after all, you want carrot to use the newer http client only in your program, not in all programs that use carrot ...

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

回答by laz

If something depends on HttpClient3.xit will not work to substitute 4.xsince they are completely different APIs. You will get runtime errors when trying to access the code that relies on 3.x.

如果有什么依赖,HttpClient3.x它就无法替代,4.x因为它们是完全不同的 API。尝试访问依赖于3.x.