Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven. Transitive dependencies

My project P depends on dependency A which depends on dependency B. My project's pom.xml file includes A as a dependency, and its jar is included in P's classpath. However, there is a NoClassDefFoundError thrown at runtime of P, which stems from missing B jars.

Shouldn't Maven have downloaded these dependencies automatically?

like image 833
Jake Avatar asked May 26 '10 17:05

Jake


People also ask

What is a transitive dependency in Maven?

Maven Dependency Tree Transitive dependency means that if A depends on B and B depends on C, then A depends on both B and C. Sometimes, transitivity brings a very serious problem when different versions of the same artifacts are included by different dependencies. It may cause version mismatch issues in runtime.

Where can I find Maven transitive dependencies?

You can get this information in the Maven Tool Window. First go to View → Tool Windows → Maven, to make sure that the Maven window is visible. The top-level elements in the tree are your direct dependencies, and the child elements are the transitive dependencies.

How do you fix transitive dependency?

Once you identify your package to be fixed using any of the above methods, to fix the transitive dependency, you must add a dependency to the updated version of the vulnerable package by adding it to the . csproj file. i.e such a vulnerable package needs to be made a direct dependency of your main project.


1 Answers

My project P depends on dependency A [with a compile scope] which depends on dependency B [with a compile scope].

Unless B is an optional dependency of A, B should be a dependency of P with a "compile(*)" scope (see the table of Dependency Scope and read the note) and should thus be available at runtime.

However, there is a NoClassDefFoundError thrown at runtime of P, which stems from missing B jars.

Since you're running the project under Eclipse, the class path is setup for you so I'll exclude a mistake at this level. This leaves us with the case of the optional dependency.

PS: A very useful tool to investigate this kind of problem is dependency:tree.

like image 105
Pascal Thivent Avatar answered Sep 25 '22 13:09

Pascal Thivent