Excluding a dependency in a Maven plugin helps avoid conflicts and manage dependencies more effectively. This is done using the <exclusions> tag inside the plugin’s <dependencies> section in the POM file.
- Prevents unwanted or conflicting dependencies
- Configured using <exclusions> in plugin dependency settings
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.
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://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-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Explanation:
- 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.