Maven 依赖版本控制和 java.lang.NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1539078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven dependency versioning and java.lang.NoClassDefFoundError
提问by Moro
I have a Java project X has dependency (in pom file) on project Y.
我有一个 Java 项目 X 依赖于项目 Y(在 pom 文件中)。
I was modifying in Y and build X,Y with Maven tool then runing X (on JBoss) without problems.
我在 Y 中修改并使用 Maven 工具构建 X,Y 然后运行 X(在 JBoss 上)没有问题。
When I added new class in Y then building with Maven (without problems), then running X, it throws java.lang.NoClassDefFoundErrorfor the new class.
当我在 Y 中添加新类然后使用 Maven 构建(没有问题),然后运行 X 时,它会抛出java.lang.NoClassDefFoundError新类。
I think its a Maven dependency versioning or something like that ... I searched mainly at Google but nothing has effect... How to resolve this problem??
我认为它是 Maven 依赖版本控制或类似的东西......我主要在谷歌搜索但没有任何效果......如何解决这个问题?
采纳答案by Moro
Ok, sorry for late info.
好的,抱歉迟到的信息。
The X package is ejb, so the X.jar has no lib directory.
X包是ejb,所以X.jar没有lib目录。
Then Y.jar should be placed in the Jboss/server/default/lib, it worked correctly.
然后Y.jar应该放在Jboss/server/default/lib下,就可以正常工作了。
Thanx for all.
谢谢大家。
回答by Pascal Thivent
Moro, you wrote in a comment that X has the following dependency declared:
Moro,您在评论中写道 X 声明了以下依赖项:
<dependency>
<groupId>Y</groupId>
<artifactId>Y</artifactId>
<scope>provided</scope>
<version>1.0</version>
</dependency>
First point. You are using a "fixed" version here (as opposed to "SNAPSHOT"). When using SNAPSHOT, maven will automatically grab the latest SNAPSHOTevery timeyou build. On the other hand, when you are using 1.0, once maven has downloaded this artifact, it nevertries to get a new 1.0. So, you should increment Y's version or, if Y is under active development (enhancements, bug fixes, etc), you should really use SNAPSHOT.
For more informations about SNAPSHOT, check out the chapter 9.3.1.2. SNAPSHOT Versionsof Sonatype's book:
第一点。您在这里使用的是“固定”版本(而不是“ SNAPSHOT”)。使用时SNAPSHOT,maven 会在你SNAPSHOT每次构建时自动抓取最新的。另一方面,当您使用 1.0 时,一旦 maven 下载了这个工件,它就不会尝试获取新的 1.0。因此,您应该增加 Y 的版本,或者,如果 Y 正在积极开发(增强、错误修复等),您应该真正使用SNAPSHOT. 有关 的更多信息SNAPSHOT,请查看章节9.3.1.2。Sonatype 的书的快照版本:
Why would you use this? SNAPSHOT versions are used for projects under active development. If your project depends on a software component that is under active development, you can depend on a SNAPSHOT release, and Maven will periodically attempt to download the latest snapshot from a repository when you run a build. Similarly, if the next release of your system is going to have a version "1.4", your project would have a version "1.4-SNAPSHOT" until it was formally released.
你为什么要用这个?SNAPSHOT 版本用于正在开发中的项目。如果您的项目依赖于正在积极开发的软件组件,您可以依赖于 SNAPSHOT 版本,当您运行构建时,Maven 将定期尝试从存储库下载最新的快照。同样,如果您的系统的下一个版本将使用“1.4”版本,那么在正式发布之前,您的项目将使用“1.4-SNAPSHOT”版本。
Second point. You are using a providedscope. According to the chapter
9.4.1. Dependency Scope:
第二点。您正在使用provided范围。根据第9.4.1章
。依赖范围:
provideddependencies are used when you expect the JDK or a container to provide them. For example, if you were developing a web application, you would need the Servlet API available on the compile classpath to compile a servlet, but you wouldn't want to include the Servlet API in the packaged WAR; the Servlet API JAR is supplied by your application server or servlet container.provideddependencies are available on the compilation classpath (not runtime). They are not transitive, nor are they packaged.
provided当您期望 JDK 或容器提供它们时,将使用依赖项。例如,如果您正在开发一个 Web 应用程序,您将需要在编译类路径上可用的 Servlet API 来编译一个 servlet,但您不希望在打包的 WAR 中包含 Servlet API;Servlet API JAR 由您的应用程序服务器或 servlet 容器提供。provided依赖项在编译类路径(不是运行时)上可用。它们不是可传递的,也不是打包的。
Is this really what you want? How are you deploying X and Y on JBoss? Shouldn't you use the default compilescope?
这真的是你想要的吗?您如何在 JBoss 上部署 X 和 Y?你不应该使用默认compile范围吗?
回答by Jim Downing
Did you run mvn installon Y after adding the new class?
mvn install添加新课程后,您是否在 Y 上运行?
回答by matt b
Did you install/deploy the new version of Y, upgrade X's dependencies for the new Y version, and re-build X?
您是否安装/部署了新版本的 Y,为新的 Y 版本升级了 X 的依赖项,并重新构建了 X?
回答by Dan Polites
Did you try mvn cleanon project Y before you built it?
在你构建它之前,你是否在项目 Y 上尝试过mvn clean?
回答by Rich Seller
Maven resolves dependencies from local and remote repositories, though IDE plugins such as m2eclipsewill also resolve dependencies that are projects within the workspace. If you don't have such a plugin, you will need to install artifact Y to a local repository or deploy it to a remote repository so that you and your peers can access it before Maven will recognise that a change has been made when building project X.
Maven 解析来自本地和远程存储库的依赖项,但 IDE 插件(例如m2eclipse)也将解析作为工作区中项目的依赖项。如果您没有这样的插件,则需要将工件 Y 安装到本地存储库或将其部署到远程存储库,以便您和您的同行可以访问它,然后 Maven 会在构建项目时识别出已进行更改X。
工作区分辨率截图 http://docs.codehaus.org/download/attachments/11403480/project-properties.png
If the two projects are closely related, you could consider creating a multi-module buildso that projects X and Y are built at the same time.

