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 :
- 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 !!