java - How to exclude a dependency from parent's project in Maven?

Java - How to exclude a dependency from parent's project in Maven?

In Maven, you can exclude a dependency inherited from the parent project by using the <exclusions> element within the <dependency> declaration in the child project's pom.xml file. This allows you to exclude specific dependencies that are defined in the parent project.

Here's an example of how to exclude a dependency in a child project:

Suppose you have the following parent project (parent-pom.xml) with a dependency:

<!-- parent-pom.xml --> <project xmlns="http://maven.apache.org/POM/4.0.0" ...> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>unwanted-dependency</artifactId> <version>1.0.0</version> </dependency> </dependencies> </project> 

Now, in your child project (child-pom.xml), you want to exclude the unwanted-dependency:

<!-- child-pom.xml --> <project xmlns="http://maven.apache.org/POM/4.0.0" ...> <parent> <groupId>com.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> </parent> <dependencies> <dependency> <groupId>org.example</groupId> <artifactId>wanted-dependency</artifactId> <version>1.0.0</version> </dependency> <!-- Exclude the unwanted-dependency from the parent --> <dependency> <groupId>org.example</groupId> <artifactId>unwanted-dependency</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.example</groupId> <artifactId>excluded-transitive-dependency</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project> 

In this example:

  1. The child project inherits from the parent project.
  2. It includes a new dependency (wanted-dependency) and explicitly excludes the unwanted-dependency from the parent, along with any transitive dependencies (excluded-transitive-dependency).

This way, the unwanted dependency is excluded, and only the specified dependencies are included in the child project.

Note: It's important to carefully manage dependencies and exclusions to avoid unexpected issues, especially when excluding transitive dependencies. Ensure that excluding a dependency won't break the functionality of your project.

Examples

  1. "Maven exclude dependency from parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencies> <dependency> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> <version>excluded-version</version> <scope>provided</scope> </dependency> </dependencies> </project> 
    • Description: Excludes a dependency from the parent's project by defining it with a provided scope in the child project.
  2. "Maven exclude transitive dependency in child project"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencies> <dependency> <groupId>some.groupId</groupId> <artifactId>some-artifactId</artifactId> <version>some-version</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> <version>excluded-version</version> </dependency> </dependencies> </dependencyManagement> </project> 
    • Description: Excludes a transitive dependency in the child project using <dependencyManagement> in the parent POM.
  3. "Maven exclude plugin dependency from parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <build> <plugins> <plugin> <groupId>excluded.groupId</groupId> <artifactId>excluded-plugin-artifactId</artifactId> <version>excluded-version</version> </plugin> </plugins> </build> </project> 
    • Description: Excludes a plugin dependency from the parent POM by specifying it in the <build> section of the child project.
  4. "Maven exclude dependency from specific profile in parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <profiles> <profile> <id>exclude-dependency-profile</id> <dependencies> <dependency> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> <version>excluded-version</version> </dependency> </dependencies> </profile> </profiles> </project> 
    • Description: Excludes a dependency from a specific profile in the parent POM, and the child project activates this profile as needed.
  5. "Maven exclude dependency with exclusion tag in child project"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencies> <dependency> <groupId>some.groupId</groupId> <artifactId>some-artifactId</artifactId> <version>some-version</version> <exclusions> <exclusion> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project> 
    • Description: Excludes a specific dependency using the <exclusions> tag within the <dependency> section in the child project.
  6. "Maven exclude dependency using properties in parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <properties> <exclude.dependency>excluded.groupId:excluded-artifactId</exclude.dependency> </properties> <dependencies> <dependency> <groupId>${exclude.dependency}</groupId> <version>excluded-version</version> </dependency> </dependencies> </project> 
    • Description: Excludes a dependency using properties defined in the parent POM, allowing flexibility in specifying dependencies to exclude.
  7. "Maven exclude dependency by type and scope in child project"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencies> <dependency> <groupId>some.groupId</groupId> <artifactId>some-artifactId</artifactId> <version>some-version</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </project> 
    • Description: Excludes a dependency by specifying its type and scope in the child project.
  8. "Maven exclude dependencies using dependencyManagement in child project"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> <version>excluded-version</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Dependencies in child project --> </dependencies> </project> 
    • Description: Excludes a dependency using <dependencyManagement> in the child project to manage dependencies in a centralized manner.
  9. "Maven exclude dependency with exclusions in dependencyManagement in parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>some.groupId</groupId> <artifactId>some-artifactId</artifactId> <version>some-version</version> <exclusions> <exclusion> <groupId>excluded.groupId</groupId> <artifactId>excluded-artifactId</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Dependencies in child project --> </dependencies> </project> 
    • Description: Excludes a dependency with exclusions specified in <dependencyManagement> in the parent POM.
  10. "Maven exclude multiple dependencies from parent POM"

    • Code:
      <project> <parent> <groupId>parent.groupId</groupId> <artifactId>parent-artifactId</artifactId> <version>parent-version</version> </parent> <dependencies> <dependency> <groupId>excluded1.groupId</groupId> <artifactId>excluded1-artifactId</artifactId> <version>excluded1-version</version> </dependency> <dependency> <groupId>excluded2.groupId</groupId> <artifactId>excluded2-artifactId</artifactId> <version>excluded2-version</version> </dependency> <!-- Add more excluded dependencies --> </dependencies> </project> 
    • Description: Excludes multiple dependencies from the parent project by listing them in the <dependencies> section of the child project.

More Tags

null-check webviewclient prometheus-operator laravel-4.2 mobile animated-webp .htaccess varbinarymax rdl event-bubbling

More Programming Questions

More Other animals Calculators

More Weather Calculators

More Biochemistry Calculators

More Auto Calculators