Maven
POM
Dependency Management
Java
Build Tools

Maven: exclude a dependency inherited from a parent POM

Codemia Knowledge Hub ·

To keep an artifact inherited from a parent POM out of your build, declare it in the child POM with an <exclusions> block that names the unwanted groupId and artifactId. Exclusions only drop transitive dependencies; a dependency the parent declares directly is overridden instead, as shown below. Then run Maven's dependency:tree to confirm what is actually resolved.

Why inherited dependencies cause problems

Child POMs inherit configuration, including dependencies, from their parent. That promotes reuse but can pull unwanted artifacts into a project, which leads to:

  1. Dependency conflicts: different versions of the same library end up on the classpath ("Jar Hell").
  2. Longer builds: extra artifacts take time to resolve and download.
  3. Bloat: the final artifact is larger than it needs to be.

Adding <exclusions> in the child POM

A POM is the file that defines a project's configuration, including how it is built and what it depends on. Maven's <exclusions> tag lets you name specific unwanted artifacts so they are left out of the build. The change goes in the child POM:

xml
1<dependencyManagement>
2    <dependencies>
3        <dependency>
4            <groupId>com.example.group</groupId>
5            <artifactId>example-artifact</artifactId>
6            <!-- Excluding specific artifact -->
7            <exclusions>
8                <exclusion>
9                    <groupId>com.unwanted.group</groupId>
10                    <artifactId>unwanted-artifact</artifactId>
11                </exclusion>
12            </exclusions>
13        </dependency>
14    </dependencies>
15</dependencyManagement>

Example: excluding log4j-core

Suppose the parent POM brings in a logging framework the child project does not need:

xml
1<dependencies>
2    <dependency>
3        <groupId>org.apache.logging.log4j</groupId>
4        <artifactId>log4j-core</artifactId>
5        <version>2.14.1</version>
6    </dependency>
7</dependencies>

An <exclusions> block cannot remove log4j-core here, because the parent declares it directly rather than pulling it in transitively. Maven has no way to un-inherit a direct dependency, so redeclare it in the child POM with provided scope. It stays on the compile classpath but is left out of the packaged artifact:

xml
1<dependencies>
2    <dependency>
3        <groupId>org.apache.logging.log4j</groupId>
4        <artifactId>log4j-core</artifactId>
5        <version>2.14.1</version>
6        <!-- Overrides the inherited compile scope -->
7        <scope>provided</scope>
8    </dependency>
9</dependencies>

mvn dependency:tree then shows log4j-core:jar:2.14.1:provided. If the child must not see it at all, move the dependency out of the parent's <dependencies> into its <dependencyManagement>, so each child opts in.

Things to check after excluding

  • Where the exclusion lives: specify it at the level of the POM hierarchy where it is needed.
  • Transitive dependencies: exclusions also apply to transitive dependencies, though deeply nested ones may need their own manual exclusion.
  • Versions: make sure any version conflicts are resolved after the exclusion.
  • The dependency tree: use dependency:tree to see the current dependencies and their hierarchy.
ConceptExplanation
Inheritance in MavenChild POM inherits configurations and dependencies from Parent POM
Need for Excluding ArtifactsTo avoid dependency conflicts, reduce build time, and prevent artifact bloat
Key Tag for Exclusion<exclusions>
Direct parent dependencyOverride with provided scope, shown with log4j-core
Best Practice RecommendationsRegular reviews, clear documentation, dependency tree analysis

Document why each exclusion exists, and review dependencies and exclusions periodically so they still match the project's needs.

Keeping inherited dependencies under control this way gives you fine-grained control over what the project pulls in, which means fewer dependency resolution problems, faster builds and a smaller final artifact.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms