Apache Maven – Transitive dependencies explanation

In this article, we will understand about the working of transitive dependencies in Apache Maven

It’s a general perception among Java developer community that Maven is a build tool, yes it is!! But in addition to this Maven helps to import project dependent JARS from central repositories if specified in the pom.xml

Not only that declaring dependencies in pom.xml helps to download its own dependency JARS as well i.e.; transitive dependencies

 

Transitive Dependency:

For example, project A depends on project B and project B depends on project C

A –> B
B –> C

Now whenever maven looks for the dependency of project A, then it downloads the direct dependency as specified i.e.; project B and also project C as project B depends on this –> that’s transitive dependency

 

Let us look at it in detail

Sample pom.xml for Maven Transitive dependencies example

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.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
	</dependencies>

</project>

In the above sample pom.xml, we have explicitly declared “spring-core-4.1.2.RELEASE” as dependencies to our “MavenTransitiveDependencies” project

But this “spring-core-4.1.2.RELEASE” dependency, in turn dependent on “common-logging-1.1.3” JAR

Maven manages this transitive dependency and downloads from central repository, if not available in the local repository

You can check this using maven command “mvn dependency:tree

1_Apache-Maven-Transitive-dependencies-dependency-tree

You can check this online as well at http://repo1.maven.org/maven/ or browse at http://search.maven.org/

2_Apache-Maven-Transitive-dependencies-online_central_repository

To be noted:

If this dependency “commons-logging” has been specified explicitly in pom.xml with latest version, then in that case maven will download this latest version

For example,

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.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

</project>

In above case, maven will download explicit version specified for “commons-logging” instead reading & downloading dependent version of “spring-core

Again, you can check this using maven command “mvn dependency:tree

3_Apache-Maven-Transitive-dependencies-dependency-tree_latest

 

Useful Eclipse IDE shortcuts :

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Apache Maven - Exclusion of Transitive dependencies
Apache Maven - Installing custom library into local repository