简体   繁体   中英

Override dependencies of third party jar in maven

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

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

Any suggestions will be appreciated..

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

We can define " the exclusion " for each dependency as it is mentioned at 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.

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

Add a dependency on HttpClient 4.1.1 to your POM. 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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM