Java Eclipse Web 项目依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1693322/
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 Web Project Dependencies
提问by LiorH
This question is about Eclipse development, I am trying to create a web application in Eclipse, the problem is that I don't get the the dependencies jars to the deployed archive.
这个问题是关于 Eclipse 开发的,我正在尝试在 Eclipse 中创建一个 Web 应用程序,问题是我没有将依赖项 jars 获取到已部署的存档中。
I've created a new dynamic web project and linked it to another Java project. The Java project references a few 3rd party jars (e.g. Spring jars) but for some reasons when publishing the web project I get only the Java project jar in the lib dir of the war, without the Java project dependencies (e.g.Spring).
我创建了一个新的动态 Web 项目并将其链接到另一个 Java 项目。Java 项目引用了一些 3rd 方 jar(例如 Spring jar),但由于某些原因,在发布 web 项目时,我只在 war 的 lib 目录中获得了 Java 项目 jar,而没有 Java 项目依赖项(例如 Spring)。
In other words, I have project A (web project) that depends on project B (Java Project), project B depends on Spring jars. When I publish the web project as a war to JBoss only project B is packaged into the jar (no spring jars)
换句话说,我有项目 A(Web 项目)依赖于项目 B(Java 项目),项目 B 依赖于 Spring jar。当我将 web 项目作为 war 发布到 JBoss 时,只有项目 B 被打包到 jar 中(没有 spring jars)
I know I can do it with ant, I even have such build.xml
to build the whole app, but I thought eclipse can also perform the packaging task for me.
我知道我可以用 ant 来完成,我什至有这样的能力build.xml
来构建整个应用程序,但我认为 eclipse 也可以为我执行打包任务。
I added the Java project to the Java EE module dependencies in the web project.
我将 Java 项目添加到 Web 项目中的 Java EE 模块依赖项中。
Should I use the export option in the Java project build path properties? should I add the dependencies of the Java project to the web project as well?
我应该在 Java 项目构建路径属性中使用导出选项吗?我应该将 Java 项目的依赖项也添加到 web 项目中吗?
What am I doing wrong?
我究竟做错了什么?
Edit: I am using Eclipse 3.5.1
编辑:我使用 Eclipse 3.5.1
采纳答案by Rod Lima
- Go to web project properties.
- Deployment Assembly (Left).
- Add > Select project > Select your lib project > Check "Assemble projects into the WEB-INF/lib folder of the web application" if not checked > Finish.
- Java Build Path (Left) > Projects Tab > Add... > Select your lib project > OK
- Java Build Path (Left) > Order and Export Tab > Select your lib project.
- Save
- Now it works.
- 转到 Web 项目属性。
- 部署组件(左)。
- 添加 > 选择项目 > 选择您的 lib 项目 > 如果未选中,请选中“将项目组装到 Web 应用程序的 WEB-INF/lib 文件夹中”> 完成。
- Java 构建路径(左)> 项目选项卡> 添加...> 选择您的 lib 项目> 确定
- Java 构建路径(左)> 订购和导出选项卡 > 选择您的 lib 项目。
- 节省
- 现在它起作用了。
回答by duffymo
You should be packaging your web app into a WAR file prior to deploying it. There must be a way to tell Eclipse which JAR files you'd like it to put in the WEB-INF/lib directory. If not, write an Ant build.xml that does it for you.
在部署 Web 应用程序之前,您应该将它打包到一个 WAR 文件中。必须有一种方法来告诉 Eclipse 您希望将哪些 JAR 文件放入 WEB-INF/lib 目录中。如果没有,请编写一个 Ant build.xml 来为您完成。
You should not have to modify any classpath variables in either your environment or the app server. The right way to do it is to create a proper WAR file.
您不应该修改您的环境或应用程序服务器中的任何类路径变量。正确的做法是创建一个适当的 WAR 文件。
UPDATE: I re-read your question:
更新:我重新阅读了你的问题:
but for some reasons when publishing the web project I get only the java project jar in the lib dir of JBoss, without its dependencies jar (e.g.Spring).
但是由于某些原因,在发布 web 项目时,我在 JBoss 的 lib 目录中只得到了 java 项目 jar,没有它的依赖 jar(例如 Spring)。
What does this mean? You should not be putting anything in the JBOSS /lib dir. The whole point of a WAR file is that it's a self-containing artifact that carries along all its dependencies. If you're trying to write stuff to the JBOSS /lib directory, you're doing it wrong.
这是什么意思?您不应该在 JBOSS /lib 目录中放置任何东西。WAR 文件的全部意义在于它是一个自包含的工件,带有它的所有依赖项。如果您尝试将内容写入 JBOSS /lib 目录,那么您就做错了。
The WAR file needs to go into the domain that you set up. No need to alter the app server.
WAR 文件需要进入您设置的域。无需更改应用服务器。
回答by Thorbj?rn Ravn Andersen
If I understand you correctly the problem is that if you use project A which in turn depends on project B you do not get the artifacts from project B but only A.
如果我理解正确的话,问题是如果您使用项目 A,而项目 A 又取决于项目 B,则您不会从项目 B 获得工件,而只能从 A 获得工件。
The issue is that your project must list ALL the things it needs in Properties -> Java EE module dependencies, which is a separate mechanism from the usual project exports. You will probably need to do some experimentation.
问题是您的项目必须在 Properties -> Java EE 模块依赖项中列出它需要的所有内容,这是与通常的项目导出不同的机制。您可能需要进行一些实验。
回答by BalusC
I added the Java project to the Java EE module dependencies in the web project.
我将 Java 项目添加到 Web 项目中的 Java EE 模块依赖项中。
If the purpose is to take the dependencies (read: JAR files/projects/etc) of the otherproject into the runtime classpath of the currentproject, then only that way doesn't work. You need to configure the otherproject to exportits dependencies. It's done by Order and Export tab in build path properties. Hope this helps.
如果目的是将其他项目的依赖项(读取:JAR 文件/项目/等)放入当前项目的运行时类路径中,那么只有这种方式是行不通的。您需要配置另一个项目以导出其依赖项。它由构建路径属性中的 Order 和 Export 选项卡完成。希望这可以帮助。
回答by Stijn de Witt
Also take note of this bug reported on Eclipse WTP, which sometimes prevent your dependencies from being deployed to WEB-INF/lib, even if you configured everything correctly. Sigh.
还要注意 Eclipse WTP 上报告的这个错误,它有时会阻止您的依赖项部署到 WEB-INF/lib,即使您正确配置了所有内容。叹。
Bug 312897 - Deployment feature doesn't deploy classpath dependencies.
As I understand it, to use a JAR at compile time in Eclipse and make sure it is also available at run-time in the appserver, you:
据我了解,要在 Eclipse 中的编译时使用 JAR 并确保它在运行时在 appserver 中也可用,您:
- Add the JAR to your build path
- Mark the JAR as an exported EE module dependency
- 将 JAR 添加到您的构建路径
- 将 JAR 标记为导出的 EE 模块依赖项
If you need JAR's which are needed by another project that you are using, I would personally list them as explicit dependencies in the build path and also add those to the exported module dependencies.
如果您需要正在使用的另一个项目所需的 JAR,我个人会将它们列为构建路径中的显式依赖项,并将它们添加到导出的模块依赖项中。
But don't take my answer at face value as I stumbled upon this post while searching for the reason I'm getting NoClassDefFound errors in my Eclipse project... ;)
但是,当我在寻找我在 Eclipse 项目中遇到 NoClassDefFound 错误的原因时偶然发现了这篇文章时,请不要轻信我的答案......;)
回答by George
I know it's been four years since the last post, and maybe this option wasn't available back then, but here's what worked for me:
我知道自上次发布以来已经四年了,也许当时这个选项不可用,但以下是对我有用的方法:
- Go to Web Project->Properties
- Deployment Assembly
- Use the 'Add' button to add your project dependency
- Go to Project->Properties for the project you're depending on
Deployment Assembly
- Use the 'Add' button to add the project's runtime library dependencies
- Web Project->Run As..->Run On Server and it worked for me
- 转到 Web 项目-> 属性
- 部署组装
- 使用“添加”按钮添加您的项目依赖项
- 转到您依赖的项目的 Project->Properties
Deployment Assembly
- 使用“添加”按钮添加项目的运行时库依赖项
- Web 项目-> 运行方式..-> 在服务器上运行,它对我有用
Be warned that steps 4-6 will mean that if you're using eclipse to build your jar file, it will now include your dependent libraries in that jar.
请注意,步骤 4-6 意味着如果您使用 eclipse 构建您的 jar 文件,它现在将在该 jar 中包含您的依赖库。
Thanks to rodgarcialima for helping me get to step 3.
感谢 rodgarcialima 帮助我进入第 3 步。