Horje
Exclude a Dependency in a Maven Plugin

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.





Reffered: https://www.geeksforgeeks.org


Advance Java

Related
Maven Repositories Maven Repositories
Setting the Java Version in Maven Setting the Java Version in Maven
Why Maven Doesn’t Find JUnit Tests to Run? Why Maven Doesn’t Find JUnit Tests to Run?
Load Balancing for WebSockets in Spring Boot Load Balancing for WebSockets in Spring Boot
How to Resolve &quot;Spring Security hasRole() not working&quot;? How to Resolve &quot;Spring Security hasRole() not working&quot;?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15