Eclipse Web Project Dependencies

蓝咒 提交于 2019-11-26 11:02:12

问题


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.

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

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)

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.

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

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?

What am I doing wrong?

Edit: I am using Eclipse 3.5.1


回答1:


  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.



回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.

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.

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

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.

The WAR file needs to go into the domain that you set up. No need to alter the app server.




回答6:


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.

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.



来源:https://stackoverflow.com/questions/1693322/eclipse-web-project-dependencies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!