Updated April 12, 2023
Introduction to Maven Exclude Dependency
Maven is a building tool that is most often used in java projects for defining and maintaining the build process. Besides that maven provides us with full-proof dependency management features that help in building a single module and multimodule building of projects and deploying the same. Maven has maintained a central repository where all the jars and Javadoc’s are available and can be added by adding the dependency tag corresponding to your dependency in your pom.xml which is called a direct dependency specification. Besides that, there are some dependencies of the dependencies that you have added in your pom.xml. This is called transitive dependencies that are automatically added by maven. In this article, we will discuss transitive dependencies and exclusion of dependencies and see how certain dependency can be excluded in maven with the help of an example.
Transitive Dependencies in Maven
The dependencies that are required by the dependencies specified by you in pom.xml are automatically included by maven by reading all the project files of your dependencies from the remote repository and adding them. This transitive dependency addition can be done up to any level that means adding dependencies of specified dependencies in pom and then further adding dependencies of that dependencies and so on unless and until the cyclic dependency occurs. This is called as transitive dependencies. This leads to the addition of a huge set of dependencies. To minimize this some of the strategies that are followed include – Dependency mediation, Dependency Management, Dependency Scope, Excluded Dependencies, Optional Dependencies. One of the transitive dependency management strategies is to exclude the dependency in maven.
Exclude Dependencies
Whenever transitive dependencies come into the picture, there may be version mismatch issues between the dependencies of the artifacts or mismatch in versions of our project artifact and the deployment platform’s artifact. This can be managed by excluding such dependencies and breaking the transitive dependency by using the <exclusion> tag. The exclusion of dependencies is made on the basis of a per-dependency basis instead of the pom level for making sure that the dependency graph and flow are predictable. The unwanted dependencies are excluded from the project’s classpath when maven resolves the transitive dependencies by using the exclusion technique. The excluded dependencies artifact is not added in your project’s classpath based on the way in which the dependencies will be declared for exclusion. This can be done for problematic jars and jars that are of older versions and not compatible with your project’s java version or jars that are not compatible with any other dependencies in your project.
Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom.xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag. Let us see how we can exclude the transitive dependency we want with the help of an example.
Consider that we have Junit 4.12 and dbunit dependency tags defined in the pom.xml as direct dependencies. Out of them, dbunit dependency has the transitive dependency of JUnit. Hence, it will download JUnit 3.8.2 dependency as its transitive dependency. In order to exclude the JUnit 3.8.2 dependency in your pom.xml, you can mention that in the exclusion tag inside exclusions tag inside the dbuint dependency tag that you are using because JUnit 3.8.2 is the transitive dependency that will arrive and be downloaded because of mentioning the direct dependency of dbunit. The pom.xml should contain the following snippet in that case:
Code:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>${dbunit.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
In the above case, we specify that JUnit 4.12 dependency should always be added in your maven project and the version of JUnit that will be downloaded as the transitive dependency of the dbunit will be excluded as we have mentioned so inside the dbunit dependency tag to exclude the transitive dependency that will be downloaded with artifact id as JUnit and groupid as JUnit.
Ignoring the Dependencies in Pom
During the build lifecycle of maven, dependency:analyze-only goal if bound then maven performs the analysis of dependencies during the verify phase of the lifecycle. We can exclude the dependencies that are not declared but used or the dependencies that are used but not declared using maven plugins. These jars or dependencies are generally ignored when the byte-code analysis is failed usually when dependencies contain the annotations. These dependencies can be ignored or excluded by using the ignoredDependencies, ignoredUsedUndeclaredDependencies, and ignoredUnusedDeclaredDependency tags inside the configuration tag in the execution tag of the plugin tag in pom.xml. For example, consider the following pom.xml where ignoredDependencies tag is used to exclude the dependency when used but undeclared or declared but not used case for Junit dependency. The jFormatString dependency is excluded or ignored when it is used but undeclared while junit-ui-runners dependency is excluded or ignored when it is been declared but not used. The value inside ignoredDependencies, ignoredUsedUndeclaredDependencies, and ignoredUnusedDeclaredDependency tags is declared in the format of groupid:artifactid.
Code:
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>analyze</id>
<goals>
<goal>analyze-only</goal>
<!-- goal can be analyze or analyze-only depndending on the compilation way that you wish to perform. -->
</goals>
<configuration>
<failOnWarning>true</failOnWarning>
<ignoredDependencies>
<ignoredDependency>junit:junit</ignoredDependency>
</ignoredDependencies>
<ignoredUsedUndeclaredDependencies>
<ignoredUsedUndeclaredDependency>com.google.code.findbugs:jFormatString</ignoredUsedUndeclaredDependency>
</ignoredUsedUndeclaredDependencies>
<ignoredUnusedDeclaredDependencies>
<ignoredUnusedDeclaredDependency>junit:junit-ui-runners</ignoredUnusedDeclaredDependency>
</ignoredUnusedDeclaredDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Conclusion
The dependencies that are direct dependencies specified inside the pom.xml can be excluded or ignored using ignoredDependencies, ignoredUsedUndeclaredDependencies, and ignoredUnusedDeclaredDependency tags in the pom.xml. While the transitive dependencies that are the dependencies required by the direct dependencies in pom.xml and are included automatically by maven can be managed by using various strategies. One of them is excluding the transitive dependencies with the help of <exclusion> tag inside <exclusions> tag. Exclusion of dependency helps in decreasing the amount of the transitive dependencies that get added in the maven project and reduce the exponential curve of transitive dependency addition.
Recommended Articles
We hope that this EDUCBA information on “Maven Exclude Dependency” was beneficial to you. You can view EDUCBA’s recommended articles for more information.