Maven

Maven Plugin Exclude Dependency

1. Introduction

When working with Maven, you may encounter a situation where you need to exclude a dependency from a plugin. This could be necessary if a plugin has a dependency that is conflicting with another dependency in your project or if you simply want to remove a dependency that you don’t need. In this article, we’ll discuss how to exclude a dependency from a Maven plugin.

2. Maven

Maven is a popular build automation tool used for managing dependencies in Java projects. Maven manages the transitivity of dependencies, which means it can automatically add all the required dependencies by the dependencies we added. However, in some cases, this transitivity can quickly inflate the number of dependencies because it adds cascading dependencies.

Dependency exclusion is a common operation in Maven that allows us to exclude sub-dependencies that are not needed in a project. For instance, if a project only uses a small part of a dependency that does not need a sub-dependency, we can exclude that sub-dependency. This helps reduce the number of dependencies in a project.

3. Excluding a Transitive Dependency in Maven

To exclude sub-dependencies, we can use the <exclusions> element in the <dependency> element of the POM file. For example, suppose we want to exclude the commons-lang3 sub-dependency from the commons-text transitivity chain in our project. In that case, we can add a <exclusions> section in the declaration of the dependency to commons-text in the POM file of our project like below:


    org.apache.commons
    commons-text
    1.1
    
        
            org.apache.commons
            commons-lang3
        
    

By adding the <exclusion> element, we can tell Maven to ignore the commons-lang3 dependency in commons-text. Therefore, if we rebuild the project with the POM above, we’ll see that the commons-text library is integrated into our project, but not the commons-lang library.

Fig. 1: Maven Exclude Dependency.
Fig. 1: Maven Exclude Dependency.

4. Excluding a Transitive Dependency from a Maven Plugin

Excluding direct dependencies from a plugin in Maven is not supported. However, we can exclude a direct dependency from a Maven plugin by overriding it with a dummy. To do this, we must create a dummy module that must be part of our project’s root POM. This module will contain only a POM file that looks like this:


    4.0.0
    org.apache.maven.surefire
    surefire-junit47
    dummy

Next, we need to adapt our child’s POM where we wish to deactivate the dependency. To do so, we must add the dependency with the dummy version to the Maven Surefire plugin declaration:


    org.apache.maven.plugins
    maven-surefire-plugin
    ${surefire-version}
    
        alphabetical
        1
        
            
                junit
                false
            
        
    
    
         
            org.apache.maven.surefire
            surefire-junit47
            dummy
        
    

By adding the dummy dependency, we can tell Maven to deactivate the JUnit 4.7 dependency of the Maven Surefire plugin. Once we build the project, we will see that the JUnit 4.7 dependency of the Maven Surefire plugin has not been included in the project, and the exclusion has worked well.

5. Conclusion

Dependency management is an essential aspect of any software development project. In Java projects, the most commonly used dependency management tool is Apache Maven. Maven allows developers to define project dependencies in a single configuration file called a Project Object Model (POM). When a project is built, Maven automatically downloads the required dependencies from remote repositories and includes them in the project build.

In many cases, a project’s dependencies may have their own dependencies, which are called transitive dependencies. Maven automatically resolves transitive dependencies and includes them in the project build. However, sometimes a project may not need all of the transitive dependencies, or a dependency may conflict with another dependency in the project. In such cases, it becomes necessary to exclude certain dependencies from the project build.

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button