问题描述
我的项目 P 依赖于依赖项 A,而依赖项 A 又依赖于依赖项 B.我的项目的 pom.xml 文件包含 A 作为依赖项,并且它的 jar 包含在 P 的类路径中.但是,在 P 的运行时会抛出一个 NoClassDefFoundError
,这是由于缺少 B jar.
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.
Maven 不应该自动下载这些依赖项吗?
Shouldn't Maven have downloaded these dependencies automatically?
推荐答案
除非 B 是 可选依赖 应该是 P 的依赖项,具有compile(*)"作用域(参见 Dependency 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.
但是,在 P 的运行时会抛出 NoClassDefFoundError,这是由于缺少 B jar.
由于您是在 Eclipse 下运行项目,因此已为您设置了类路径,因此我将排除此级别的错误.这给我们留下了可选依赖的情况.
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:调查这类问题的一个非常有用的工具是dependency:tree
.
PS: A very useful tool to investigate this kind of problem is dependency:tree
.
这篇关于马文.传递依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!