Eclipse 调试时类路径问题:如何将依赖项目的输出包含到 Web 项目的运行时类路径中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/326689/
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
Eclipse debug-time classpath problem: How do you include a dependent project's output into a web project's runtime classpath?
提问by Brian Deacon
So I started with a web services project (just a dynamic web project) that builds and debugs correctly from eclipse. We've pulled a chunk of common code out that we want to put into a shared library so now those classes are going into a separate jar project that the web project references.
所以我从一个从 Eclipse 正确构建和调试的 Web 服务项目(只是一个动态 Web 项目)开始。我们已经提取了一大块我们想要放入共享库的通用代码,所以现在这些类将进入 Web 项目引用的单独 jar 项目。
On the web project, I did Project->Properties->Java Build Path->Projects->Add and added the jar project. And this correctly solved all the compile-time classpath problems and everything builds fine. But at runtime, when the tomcat server fires up, spring attempts to inject some of the classes contained in the jar file and I get a NoClassDefFoundError.
在web项目上,我做了Project->Properties->Java Build Path->Projects->Add并添加了jar项目。这正确解决了所有编译时类路径问题,并且一切正常。但是在运行时,当 tomcat 服务器启动时,spring 尝试注入 jar 文件中包含的一些类,我得到一个 NoClassDefFoundError。
My .class and properties files and the contents of my META-INF directory are showing up in the ./build directory, but my WEB-INF/lib directory seems to be referenced in-place, and the jar dependency doesn't get copied in to it to show up as part of the Web App Library.
我的 .class 和属性文件以及我的 META-INF 目录的内容显示在 ./build 目录中,但我的 WEB-INF/lib 目录似乎被就地引用,并且没有复制 jar 依赖项以显示为 Web 应用程序库的一部分。
What is the magical incantation to tell eclipse that the other jar project needs to be available to tomcat at runtime? From our ant build script, we first just build the other project into WEB-INF/lib and everything works fine, but not for eclipse debugging.
告诉 eclipse 另一个 jar 项目需要在运行时对 tomcat 可用的神奇咒语是什么?从我们的 ant 构建脚本中,我们首先将另一个项目构建到 WEB-INF/lib 中,一切正常,但不适用于 eclipse 调试。
采纳答案by Vineet Reynolds
Java EE module dependencies would solve this problem. You have already done the task of extracting your common classes into its own project, possiblybecause other projects depend on these classes. Either way, you'll have to ensure that this is a Utility project (appears under Java EE in the project wizards), and not just a plain Java project. One that is done, you can proceed to add the Utility project to your build path (compile-time path) as you have figured out.
Java EE 模块依赖可以解决这个问题。您已经完成了将公共类提取到其自己的项目中的任务,可能是因为其他项目依赖于这些类。无论哪种方式,您都必须确保这是一个实用程序项目(出现在项目向导中的 Java EE 下),而不仅仅是一个普通的 Java 项目。完成后,您可以继续将 Utility 项目添加到您的构建路径(编译时路径),正如您所想的那样。
The additional (final) step is to establish a Java EE module dependency between your Dynamic Web project and the shared library, which causes the utility's classes to be placed in WEB-INF\lib during deployment, and even during export of the WAR. To do so, visit the dynamic web project's properties, and browse to the Java EE module dependencies. Ensure that your utility project is selected here. Redeploy/publish your application and you should be good to go.
附加(最后)步骤是在您的动态 Web 项目和共享库之间建立 Java EE 模块依赖关系,这会导致在部署期间,甚至在导出 WAR 期间,将实用程序的类放置在 WEB-INF\lib 中。为此,请访问动态 Web 项目的属性,并浏览到 Java EE 模块依赖项。确保在此处选择了您的实用程序项目。重新部署/发布您的应用程序,您应该一切顺利。
回答by Anoop Isaac
I figured this out after spending some time on it. If you are in Eclipse Helios , go to properties > deployment assembly > add > project
and select the dependent project you wish to add.
我花了一些时间之后才明白这一点。如果您在 Eclipse Helios 中,请转到properties > deployment assembly > add > project
并选择您要添加的依赖项目。