Apache Maven – Exclusion of Transitive dependencies

In previous article, we have learnt about what is transitive dependency. Here, we will understand how we can force maven to stop downloading transitive dependencies or exclude of transitive dependencies

Maven automatically downloads the transitive dependencies from repository as in order(local–>central–>remote) but this sometime causes issues either at compile-time or at run-time due to presence of two or more conflicting versions of the same JAR

To avoid this conflicting version issues, maven provides a mechanism to exclude certain or all transitive dependencies. Use <exclusions> & <exclusion> tags to exclude transitive dependencies specifying it’s <groupId> and <artifactId>

1. Sample example to exclude spring transitive dependencies:

  • In below example, we have explicitly specified to tell maven that exclude spring dependencies while downloading transitive dependencies using <exclusions>/<exclusion> tags

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>in.bench.resources</groupId>
	<artifactId>MavenTransitiveDependencies</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>MavenTransitiveDependencies</name>
	<description>Apache Maven (3.2.3): Transitive dependencies explanation</description>

	<!-- spring-core dependencies -->
	<dependencies>
		<dependency>
			<groupId>org.jvnet.jax-ws-commons.spring</groupId>
			<artifactId>jaxws-spring</artifactId>
			<version>1.9</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-beans</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-context</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-core</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>spring-web</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

</project>

2. Wild-card exclusion of Transitive dependencies

  • To exclude all spring dependencies at one go, instead specifying each <exclusion> tag one-by-one –> use wild-card (*) character for <artifactId>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>
	<groupId>in.bench.resources</groupId>
	<artifactId>MavenTransitiveDependencies</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>MavenTransitiveDependencies</name>
	<description>Apache Maven (3.2.3): Transitive dependencies explanation</description>

	<!-- spring-core dependencies -->
	<dependencies>
		<dependency>
			<groupId>org.jvnet.jax-ws-commons.spring</groupId>
			<artifactId>jaxws-spring</artifactId>
			<version>1.9</version>
			<exclusions>
				<exclusion>
					<groupId>org.springframework</groupId>
					<artifactId>*</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

</project>

In above case, maven downloads transitive dependencies with version “3.2.3.RELEASE” of spring framework. But we intended to specify explicitly the latest spring versions like “4.1.2.RELEASE”, thereby avoiding conflicting version issues

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Dependency Scopes
Apache Maven - Transitive dependencies explanation