25

这个问题是关于 Eclipse 开发的,我正在尝试在 Eclipse 中创建一个 Web 应用程序,问题是我没有将依赖项 jar 放到已部署的存档中。

我创建了一个新的动态 Web 项目并将其链接到另一个 Java 项目。Java 项目引用了一些 3rd 方 jars(例如 Spring jars),但由于某些原因,在发布 web 项目时,我只在 war 的 lib 目录中获得 Java 项目 jar,没有 Java 项目依赖项(例如 Spring)。

换句话说,我有项目 A(Web 项目)依赖于项目 B(Java 项目),项目 B 依赖于 Spring jar。当我将 web 项目作为对 JBoss 的战争发布时,只有项目 B 被打包到 jar 中(没有 spring jar)

我知道我可以用 ant 来做,我什至有这样的东西build.xml来构建整个应用程序,但我认为 eclipse 也可以为我执行打包任务。

我将 Java 项目添加到 web 项目中的 Java EE 模块依赖项中。

我应该在 Java 项目构建路径属性中使用导出选项吗?我是否也应该将 Java 项目的依赖项添加到 Web 项目中?

我究竟做错了什么?

编辑:我正在使用 Eclipse 3.5.1

4

6 回答 6

52
  1. Go to web project properties.
  2. Deployment Assembly (Left).
  3. Add > Select project > Select your lib project > Check "Assemble projects into the WEB-INF/lib folder of the web application" if not checked > Finish.
  4. Java Build Path (Left) > Projects Tab > Add... > Select your lib project > OK
  5. Java Build Path (Left) > Order and Export Tab > Select your lib project.
  6. Save
  7. Now it works.
于 2011-06-29T03:47:27.523 回答
4

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:

  1. Go to Web Project->Properties
  2. Deployment Assembly
  3. Use the 'Add' button to add your project dependency
  4. Go to Project->Properties for the project you're depending on
  5. Deployment Assembly
  6. Use the 'Add' button to add the project's runtime library dependencies
  7. Web Project->Run As..->Run On Server and it worked for me

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.

Thanks to rodgarcialima for helping me get to step 3.

于 2013-07-09T21:02:30.983 回答
2

I added the Java project to the Java EE module dependencies in the web project.

If the purpose is to take the dependencies (read: JAR files/projects/etc) of the other project into the runtime classpath of the current project, then only that way doesn't work. You need to configure the other project to export its dependencies. It's done by Order and Export tab in build path properties. Hope this helps.

于 2009-11-07T18:19:49.343 回答
2

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.

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:

  • Add the JAR to your build path
  • Mark the JAR as an exported EE module dependency

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.

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... ;)

于 2010-09-14T19:28:16.680 回答
0

您应该在部署 Web 应用程序之前将其打包到 WAR 文件中。必须有一种方法可以告诉 Eclipse 您希望将哪些 JAR 文件放在 WEB-INF/lib 目录中。如果没有,请编写一个 Ant build.xml 来为您完成它。

您不必修改环境或应用服务器中的任何类路径变量。正确的做法是创建一个适当的 WAR 文件。

更新:我重新阅读了您的问题:

但是由于某些原因,在发布 web 项目时,我只在 JBoss 的 lib 目录中获得了 java 项目 jar,没有它的依赖项 jar(例如 Spring)。

这是什么意思?你不应该在 JBOSS /lib 目录中放任何东西。WAR 文件的全部意义在于它是一个包含所有依赖项的自包含工件。如果您尝试将内容写入 JBOSS /lib 目录,那么您做错了。

WAR 文件需要进入您设置的域。无需更改应用服务器。

于 2009-11-07T15:05:54.340 回答
0

如果我理解正确,问题是如果您使用项目 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.

于 2009-11-07T15:43:23.437 回答