Maven exclude transitive dependency of a transitive dependency

dependencies, dependency-management, maven

Solution

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.

Problem

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?

Original source