Maven exclude transitive dependency of a transitive dependency

Tag: maven , dependencies , dependency-management Author: daiyuezhi001 Date: 2013-12-29

Is there a syntactic way in a pom file to exclude a dependency of a transitive dependency.

For example, if A has a dependency B and B has a dependency C and C has a dependency on D, a way to exclude dependency D when compiling A.

Exclusions for a dependency only seem to go one level deep.

How I have accomplished this in the past is to include dependency C in A's pom and then add the exclusion for D in C's dependency declaration. Is this the recommended way?

Best Answer

You can add an <exclusions> element for D to the dependency B in the POM of A. Exclusions work recursively on transitive dependencies.

A good way to do this for a complex project is to use an <dependencyManagement> element in the parent POM which excludes anything you don't want to see anywhere.


Thanks for the input. I could have sworn I tried this but the dependency was still included. I was only able to exclude transitive dependencies two levels deep. So in this case I was able to exclude dependency C but not D. I will have to try again when I get a moment
It works in my case. Maybe it's a bug in an old version of Maven which has since been fixed?
Which version of maven are you using? I believe this bug was introduced in a later version of 3.0.5. Use maven version 3.0.5.
I just checked with the most recent version of Maven and it worked. When I get to work I will compare versions but it must be out of date. I will mark your answer and final. thanks