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”
You can check this online as well at http://repo1.maven.org/maven/ or browse at http://search.maven.org/
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”
Useful Eclipse IDE shortcuts :
- Eclipse IDE – How to show line numbers ?
- Eclipse IDE – How to GO TO any line number directly ?
- Eclipse IDE – How to remove unused imports ?
- Eclipse IDE – How to clean project ?
- Eclipse IDE – How to build Java project automatically ?
- Eclipse IDE – How to comment and un-comment line & block ?
- Eclipse IDE – How to generate constructor using fields ?
- Eclipse IDE – How to generate getters and setters ?
- Eclipse IDE – How to search files ?
- Eclipse IDE – How to locate methods in Java file ?
- Eclipse IDE – How to open editor using CTRL + E ?
- Eclipse IDE – Java compiler compliance level issue
Related Articles:
- Apache Maven – Introduction
- Apache Maven – Install on Windows 7 OS
- Apache Maven – Settings.xml explanation
- Apache Maven – Proxy setting explanation
- Apache Maven – pom.xml explanation
- Apache Maven – Plugins explanation
- Apache Maven – Changing default Maven repository location in Windows 7 OS
- Apache Maven – Local, Central and Remote Repositories
- Apache Maven – Installing custom library into local repository
- Apache Maven – Transitive dependencies explanation
- Apache Maven – Exclusion of Transitive dependencies
- Apache Maven – Dependency Scopes
- Apache Maven – Skipping unit test using surefire plugin
- Apache Maven – Exclusions and Inclusions of unit test
- Apache Maven – offline execution
- Apache Maven – Co-ordinates explained
- Eclipse + Maven – Integration
- Eclipse + Maven – How to import Maven project with pom.xml ?
- Eclipse + Maven – Setting M2_REPO classpath variable in IDE
- Eclipse + Maven – M2_REPO is Non Modifiable
- Eclipse + Maven – Creating and exploring projects using archetypes
- Eclipse + Maven – Converting Web project to Maven project
- Eclipse + Maven – mvn eclipse:eclipse command
- Eclipse + Maven – Plugin execution not covered by lifecycle configuration
References:
Happy Coding !!
Happy Learning !!