Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to exclude a Maven dependency globally?

I’m trying to find a “generic” way of excluding a transitive dependency from being included without having to exclude it from all the dependencies that depend on it. For example, if I want to exclude slf4j, I do the following:

  <dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-jmx</artifactId>     <version>3.3.2.GA</version>     <exclusions>       <exclusion>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-api</artifactId>       </exclusion>     </exclusions>   </dependency>   <dependency>     <groupId>org.hibernate</groupId>     <artifactId>hibernate-entitymanager</artifactId>     <version>3.4.0.GA</version>     <type>jar</type>     <exclusions>       <exclusion>         <groupId>org.slf4j</groupId>         <artifactId>slf4j-api</artifactId>       </exclusion>     </exclusions>   </dependency> 

This is partly to clean up the pom file, partly to avoid issues in the future with people adding dependencies that depend on that excluded dependency — and forgetting to exclude it.

Is there a way?

like image 810
Sébastien Le Callonnec Avatar asked Jan 17 '11 18:01

Sébastien Le Callonnec


People also ask

Is there a way to exclude Maven dependency globally?

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.

How you can exclude dependency in Maven?

You can use Exclude command from the context menu in the Maven dependency diagram to quickly exclude the specified dependency from POM and the respective tool windows. The dependency is also excluded from the Project and Maven tool windows.

How do I make Maven dependency optional?

In order to exclude these special dependencies from the main project, we can apply Maven's <optional> tag to them. This forces any user who wants to use those dependencies to declare them explicitly. However, it does not force those dependencies into a project that doesn't need them.

What is Maven exclusion?

Exclusions are set on a specific dependency in your POM, and are targeted at a specific groupId and artifactId. When you build your project, that artifact will not be added to your project's classpath by way of the dependency in which the exclusion was declared.

What is dependency exclusion in Maven?

The exclusion of dependencies is made on the basis of a per-dependency basis instead of the pom level for making sure that the dependency graph and flow are predictable. The unwanted dependencies are excluded from the project’s classpath when maven resolves the transitive dependencies by using the exclusion technique.

Does Maven resolve dependencies transitively?

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using.

How to exclude multiple transitive dependencies from a project?

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.

How to exclude unwanted dependencies from the project’s Classpath?

The unwanted dependencies can be excluded from the project’s classpath when maven resolves the transitive dependencies by using the exclusion technique. The excluded dependencies or artifacts will not be added to your project’s classpath based on the way dependencies are excluded within the <exclusion> tag.


1 Answers

Does this help? http://jlorenzen.blogspot.com/2009/06/maven-global-excludes.html

"Assuming I want to exclude avalon-framework from my WAR, I would add the following to my projects POM with a scope of provided. This works across all transitive dependencies and allows you to specify it once.

<dependencies>   <dependency>       <artifactId>avalon-framework</artifactId>       <groupId>avalon-framework</groupId>       <version>4.1.3</version>       <scope>provided</scope>   </dependency> </dependencies> 

This even works when specifying it in the parent POM, which would prevent projects from having to declare this in all child POMs."

like image 199
Joffer Avatar answered Oct 08 '22 12:10

Joffer