views:

134

answers:

2

This is a novice question.

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?

+1  A: 

If this dependency A has a compile scope - sure, it should have been downloaded and more over - made available in the classpath of the project. But if it had provided scope that would be the case, since provided deps would not be packaged with the application by Maven.

Btw how are you running this project - not running in the proper way might cause such problems as well and this is a very good guess. For example - if you're using maven exec plugin - maven will setup properly the classpath for you, but otherwise - you should setup it yourself(or build a jar with dependencies with the assembly plugin).

Bozhidar Batsov
A lists B as a dependency with "compile" scope; P lists A as a dependency with "compile" scope. B is not being added to P's classpath.
Jake
You didn't answer the question of 'how are you running it?'
bmargulies
@bmargulies Pardon. I generate Eclipse metadata and run it out of the Eclipse debugger.
Jake
You generate it with mvn eclipse:eclipse?
Bozhidar Batsov
Yes, I generate the metadata with `mvn eclipse:clean eclipse:eclipse`. A's jar is then included in the Eclipse "Referenced Libraries" for P, but no dependency of A is included (and thus B is not included either).
Jake
@Jake, gosh, that works for me.
bmargulies
+1  A: 

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.

Pascal Thivent
re the last sentence: The dependency hierarchy view in m2eclipse is a lot more user-friendly than dependency:tree, it even gives you an 'exclude' wizard
seanizer
@seanizer: True (assuming the OP is using m2eclipse). Note that [`dependency:tree`](http://maven.apache.org/plugins/maven-dependency-plugin) also supports [filtering](http://maven.apache.org/plugins/maven-dependency-plugin/examples/filtering-the-dependency-tree.html).
Pascal Thivent
I'm assuming that anybody who uses maven and eclipse together also uses m2eclipse, as it's much more powerful and comfortable than the maven-eclipse-plugin (pom editor, repository index, automatic config changes when pom was edited etc). and of course it also includes filtering: click on any artifact in the hierarchy and only this artifact's hierarchy is shown...
seanizer