Maven dependency: exclude one class

Maven dependency: exclude one class

In Maven, if you want to exclude a specific class from a dependency, you'll need to use one of the following strategies:

  1. Use a Shaded Jar with Exclusions
  2. Use a Custom Dependency Scope
  3. Exclude Transitive Dependencies

1. Use a Shaded Jar with Exclusions

This approach involves creating a shaded JAR where you can exclude specific classes from the dependencies. The maven-shade-plugin is commonly used for this.

Steps:

  1. Add the Shade Plugin to Your pom.xml:

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:your-dependency</artifact> <excludes> <exclude>your/excluded/class/path/*.class</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> 
  2. Configure the Plugin:

    • Replace your-dependency with the group and artifact ID of the dependency you want to exclude a class from.
    • Replace your/excluded/class/path/*.class with the class path you want to exclude.
  3. Build Your Project:

    • Run mvn clean package to build your project with the shaded JAR.

2. Use a Custom Dependency Scope

If the class you want to exclude is part of a transitive dependency, you can use the exclude element to exclude specific transitive dependencies. However, Maven does not support excluding individual classes directly. You can only exclude entire dependencies.

Example:

<dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>com.transitive.dependency</groupId> <artifactId>transitive-artifact</artifactId> </exclusion> </exclusions> </dependency> 

3. Exclude Transitive Dependencies

If the class you want to exclude is part of a transitive dependency, you might need to exclude the entire transitive dependency if it's possible and feasible.

Example:

<dependency> <groupId>com.example</groupId> <artifactId>example-artifact</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>com.transitive.dependency</groupId> <artifactId>transitive-artifact</artifactId> </exclusion> </exclusions> </dependency> 

Notes:

  • Shaded JAR: This approach is useful if you need to create a self-contained JAR with specific classes excluded. It involves more configuration but gives you control over the final JAR file content.

  • Transitive Dependency Exclusions: This method is easier but can only exclude entire dependencies, not individual classes.

If you specifically need to exclude a class and can't exclude an entire dependency, consider using the shading approach, which allows more granular control over the contents of the JAR file.

Examples

  1. How to exclude a specific class from a Maven dependency?

    Description: This approach shows how to exclude a class from a dependency using the maven-shade-plugin to filter out unwanted classes.

    Code:

    <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:your-dependency</artifact> <excludes> <exclude>path/to/YourClass.class</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> </plugins> </build> 

    Explanation: The maven-shade-plugin is used here to exclude a specific class from a JAR file when building your project.

  2. How to exclude a class from a transitive Maven dependency?

    Description: Excludes a class from a transitive dependency by using the maven-dependency-plugin to handle exclusions.

    Code:

    <dependency> <groupId>your.group.id</groupId> <artifactId>your-artifact-id</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>transitive.group.id</groupId> <artifactId>transitive-artifact-id</artifactId> </exclusion> </exclusions> </dependency> 

    Explanation: This code snippet excludes a transitive dependency which might contain the class you want to avoid.

  3. How to use Maven to exclude a class from a dependency at runtime?

    Description: Shows how to exclude classes at runtime using maven-dependency-plugin with an exclude configuration.

    Code:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>build-classpath</id> <phase>compile</phase> <goals> <goal>build-classpath</goal> </goals> <configuration> <excludeArtifactIds>excluded-artifact-id</excludeArtifactIds> </configuration> </execution> </executions> </plugin> 

    Explanation: This configuration excludes a specific artifact which might contain the class you want to avoid from the runtime classpath.

  4. How to exclude classes from a dependency in a shaded JAR file?

    Description: Shows how to exclude certain classes from a shaded JAR file using maven-shade-plugin.

    Code:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>*:your-dependency</artifact> <excludes> <exclude>com/yourcompany/YourClass.class</exclude> </excludes> </filter> </filters> </configuration> </execution> </executions> </plugin> 

    Explanation: This configuration removes a class from the shaded JAR that includes dependencies.

  5. How to exclude specific classes from a dependency using Maven profiles?

    Description: Use Maven profiles to conditionally exclude classes from a dependency.

    Code:

    <profiles> <profile> <id>exclude-classes</id> <dependencies> <dependency> <groupId>your.group.id</groupId> <artifactId>your-artifact-id</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>excluded.group.id</groupId> <artifactId>excluded-artifact-id</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </profile> </profiles> 

    Explanation: This profile-based approach allows you to exclude classes conditionally based on the active profile.

  6. How to exclude classes from Maven dependency using maven-assembly-plugin?

    Description: Use the maven-assembly-plugin to create an assembly with exclusions.

    Code:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifestEntries> <Class-Path>dependency1 dependency2</Class-Path> </manifestEntries> </archive> <filters> <filter> <artifact>*:your-dependency</artifact> <excludes> <exclude>path/to/YourClass.class</exclude> </excludes> </filter> </filters> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> 

    Explanation: This plugin allows you to create a JAR with specific class exclusions.

  7. How to exclude a class from Maven dependency in a multi-module project?

    Description: Exclude a class from a dependency in a multi-module Maven project.

    Code:

    <dependency> <groupId>your.group.id</groupId> <artifactId>your-artifact-id</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>excluded.group.id</groupId> <artifactId>excluded-artifact-id</artifactId> </exclusion> </exclusions> </dependency> 

    Explanation: Add this exclusion in the pom.xml of the specific module that needs the exclusion.

  8. How to exclude a class from a JAR dependency using maven-resources-plugin?

    Description: Utilize maven-resources-plugin to filter out unwanted classes.

    Code:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/classes</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/YourClass.class</exclude> </excludes> </resource> </resources> </configuration> </execution> </executions> </plugin> 

    Explanation: This configuration excludes certain classes when copying resources.

  9. How to exclude a specific class from a Maven dependency using maven-dependency-plugin?

    Description: Use maven-dependency-plugin to handle exclusions in the dependency analysis phase.

    Code:

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.3.0</version> <executions> <execution> <id>analyze</id> <phase>validate</phase> <goals> <goal>analyze</goal> </goals> <configuration> <failOnError>false</failOnError> </configuration> </execution> </executions> </plugin> 

    Explanation: This helps in identifying and handling unwanted classes during dependency analysis.

  10. How to exclude a class from an artifact in Maven using maven-dependency-tree?

    Description: Use maven-dependency-tree to visualize and exclude specific classes from artifacts.

    Code:

    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> </execution> </executions> </plugin> 

    Explanation: This plugin helps in visualizing dependencies and managing exclusions by providing detailed dependency trees.


More Tags

aio-write sonarqube cucumber scipy-spatial css-multicolumn-layout node-postgres push identity-column android-studio-3.1 yii-extensions

More Programming Questions

More Chemical thermodynamics Calculators

More Trees & Forestry Calculators

More Gardening and crops Calculators

More Various Measurements Units Calculators