0

I am looking for a way to test whether or not any of my explicit dependencies in my pom.xml reference/include any of the same transitive dependencies. For example, if dependency-A includes junit.jupiter and dependency-B also includes junit.jupiter, I want a way to see this so that I can exclude it from one of them to prevent conflicts.

I saw through this link that you can use mvn dependency:tree to essentially show all dependencies and their transitive dependencies, but it prints in a fairly unreadable format and it isn't clear through that output what the source of each transitive dependency is.

Matt C.
  • 2,330
  • 3
  • 22
  • 26
  • This is not customary practice. – chrylis -cautiouslyoptimistic- Jun 23 '21 at 04:34
  • 1
    The transitive dependencies will be merged, not duplicated. Why would you want to exclude one? – pascal Jun 23 '21 at 05:10
  • The output format of [maven-dependency-plugin](https://maven.apache.org/plugins/maven-dependency-plugin/tree-mojo.html) can be changed if you like – khmarbaise Jun 23 '21 at 07:26
  • @pascal if the two dependencies A and B both depend on different versions of junit.jupiter, that is when I would want to exclude one of the junit.jupiter's that they are bringing in. Note I would want to exclude the junit.jupiter depdency of one of them and not exclude dependency-A or dependency-B entirely. Because otherwise there would be a version mismatch/conflict – Matt C. Jun 23 '21 at 13:35

1 Answers1

2

Note that if dependency-A uses junit.jupiter and dependency-B uses junit.jupiter, then only one of these dependencies will be included. Maven will not include the same dependency twice.

What can be tricky, though, is the resulting version. Maven takes the "nearest" element, not the highest version. If you want to notice if you have conflicting versions, I recommend the "dependency convergence" rule of the maven enforcer rules.

If you want to choose one version for junit.jupiter, add an entry to <dependencyManagement> in your POM.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • To clarify, you are saying if depedency-A has a transitive dependency of junit.jupiter and dependency-B also has a transitive dependency of junit.jupiter then maven will basically completely ignore either dependency-A or dependency-B, even though they contain crucial other components that my app needs to run? Or maven will just ignore the junit.jupiter portion of one of them? – Matt C. Jun 23 '21 at 13:34
  • 1
    It will not ignore the whole dependency, but the the transitive dependency on `junit.jupiter` – J Fabian Meier Jun 23 '21 at 14:02