Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 209
arsenal Avatar asked Jun 30 '11 23:06

arsenal


People also ask

Does Maven override dependency version?

By taking advantage of Maven's nearest definition logic, developers can override the version of a dependency by declaring it on the root pom. xml file.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.


2 Answers

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.

like image 153
Charlee Chitsuk Avatar answered Sep 21 '22 23:09

Charlee Chitsuk


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

like image 35
meriton Avatar answered Sep 21 '22 23:09

meriton