Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: remove a single transitive dependency

My project includes a jar file because it is listed as a transitive dependency.

However, I have verified not only that I don't need it but it causes problems because a class inside the jar files shadows a class I need in another jar file.

How do I leave out a single jar file from my transitive dependencies?

like image 815
flybywire Avatar asked May 03 '09 11:05

flybywire


People also ask

How do you remove a transitive dependency?

If a transitive dependency exists, we remove the transitively dependent attribute(s) from the relation by placing the attribute(s) in a new relation along with a copy of the determinant.

How do you exclude transitive dependency of transitive dependency?

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.

How do I delete a dependency in Maven?

Open the pom file and click on dependency Hierarchy. Select the the jar you want to delete. Right click and click on Exclude Maven Artifact.


2 Answers

You can exclude a dependency in the following manner:

        <dependency>                 <groupId>org.springframework</groupId>                 <artifactId>spring</artifactId>                 <version>2.5.6</version>                 <exclusions>                         <exclusion>                                 <groupId>commons-logging</groupId>                                 <artifactId>commons-logging</artifactId>                         </exclusion>                 </exclusions>         </dependency> 
like image 167
David Rabinowitz Avatar answered Sep 22 '22 05:09

David Rabinowitz


The correct way is to use the exclusions mechanism, however sometimes you may prefer to use the following hack instead to avoid adding a large number of exclusions when lots of artifacts have the same transitive dependency which you wish to ignore. Rather than specifying an exclusion, you define an additional dependency with a scope of "provided". This tells Maven that you will manually take care of providing this artifact at runtime and so it will not be packaged. For instance:

    <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring</artifactId>             <version>2.5.6</version>     </dependency>     <dependency>             <groupId>commons-logging</groupId>             <artifactId>commons-logging</artifactId>             <version>1.1.1</version>             <scope>provided</scope>     </dependency> 

Side effect: you must specify a version of the artifact-to-be-ignored, and its POM will be retrieved at build-time; this is not the case with regular exclusions. This might be a problem for you if you run your private Maven repository behind a firewall.

like image 30
Pavel Avatar answered Sep 18 '22 05:09

Pavel