Is there a way to eliminate Maven dependency all over the world?

I am trying to find a “general” way to exclude a transitive dependency from an included one without having to exclude it from all its dependencies. 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 the pom file, partly to avoid future problems when people add dependencies that depend on this excluded dependency and forget to exclude it.

Is there any way?

+59
maven maven-2 dependencies
Jan 17 '11 at 18:09
source share
4 answers

Does it 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 POM projects with a scope. This works in 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 will prevent projects from declaring this in all child POMs.

+47
Jan 17 '11 at 19:31
source share

I created an empty jar and created this dependency:

 <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <scope>system</scope> <systemPath>${basedir}/src/lib/empty.jar</systemPath> <version>0</version> </dependency> 

This is not ideal, because from now on you have an empty jar in your compilation / test path. But this is just cosmetics.

+14
Jun 27 '13 at 12:59 on
source share

As a reminder, here is the answer from Maven's official documentation:

Why exceptions are thrown based on dependency and not at the POM level

This is mainly done in order to make sure that the dependency graph is predictable and to preserve the effects of inheritance from eliminating the dependency, which should not be excluded. If you go to the last resort and should be excluded, you must be absolutely sure which of your dependencies makes this unwanted transitive dependency.

If you want to make the build more reliable, you can use a range of versions . This ensures that no newer version of the dependency can interfere with the project.

 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>[1.4.2,)</version> <scope>provided</scope> </dependency> 

Any version of slf4j-api> = 1.4.2 will be considered as proposed (provided) at runtime by either the JDK itself or the container.

References

+3
Oct 11 '16 at 14:34
source share

Expand dnault's comment :

You can use the Maven Enforcer Banned Dependencies rule plugin to ensure that dependencies are excluded. You still need to exclude them manually, but the assembly will fail if someone adds the dependency elsewhere by mistake.

 <dependencies> <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> </dependencies> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-enforcer-plugin</artifactId> <version>1.4.1</version> <executions> <execution> <goals> <goal>enforce</goal> </goals> <configuration> <rules> <bannedDependencies> <excludes> <exclude>org.slf4j:slf4j-api</exclude> </excludes> </bannedDependencies> </rules> </configuration> </execution> </executions> </plugin> </plugins> 

There is also an open feature request: MNG-1977 global dependency exceptions

0
Sep 07 '17 at 20:22
source share



All Articles