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:
- Dependency conflicts: different versions of the same library end up on the classpath ("Jar Hell").
- Longer builds: extra artifacts take time to resolve and download.
- 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:
Example: excluding log4j-core
Suppose the parent POM brings in a logging framework the child project does not need:
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:
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:treeto see the current dependencies and their hierarchy.
| Concept | Explanation |
| Inheritance in Maven | Child POM inherits configurations and dependencies from Parent POM |
| Need for Excluding Artifacts | To avoid dependency conflicts, reduce build time, and prevent artifact bloat |
| Key Tag for Exclusion | <exclusions> |
| Direct parent dependency | Override with provided scope, shown with log4j-core |
| Best Practice Recommendations | Regular 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.
Related reading
- Is there auto type inferring in Java?
- Is this a new sorting algorithm? with Java and Pseudo-code implementation
- Is web.xml required to deploy a spring boot application
- Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
- Iterating through a list in reverse order in java
- Iteration order of HashSet
- Jackson - Deserialize using generic class
- Jackson date-format for OffsetDateTime in Spring Boot
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.