Open In App

Exclude a Dependency in a Maven Plugin

Last Updated : 02 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Excluding a dependency in a Maven plugin can be necessary to manage dependencies effectively and prevent conflicts or redundancies in the project. In this article, we will learn how to exclude a dependency in a Maven plugin using the <exclusions> tag within the <dependencies> section of the plugin configuration in the project's POM (Project Object Model) file.

Prerequisites:

  • Java Development Kit
  • Maven Installation
  • Integrated Development Environment

Tools and Technologies:

  • Maven Repository
  • POM File
  • Maven Plugins
  • Maven Archetypes
  • Build Life cycle

Implementation to Exclude a Dependency in a Maven Plugin

In Maven, you might want to exclude a transitive dependency included via a plugin to avoid version conflicts or redundant libraries. This can be done using the <exclusions> tag in the POM file.

Let's assume you have a Maven project that uses the maven-compiler-plugin, and you want to exclude a specific dependency commons-logging that is pulled in transitively by another library.

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>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.plexus</groupId>
                        <artifactId>plexus-utils</artifactId>
                        <version>3.0.24</version>
                        <exclusions>
                            <exclusion>
                                <groupId>commons-io</groupId>
                                <artifactId>commons-io</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                </dependencies>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


  • The maven-compiler-plugin is configured to compile the source code.
  • A specific version of the plexus-utils dependency is included.
  • The commons-io transitive dependency is excluded to avoid conflicts.



Next Article
Article Tags :

Similar Reads