Packaging JAR file in WAR file

℡╲_俬逩灬. 提交于 2019-12-23 21:07:28

问题


I have a series of dependent Java projects. I'd like to package them together into a single JAR file to be used in my WAR file. These projects depend on a large number of external libraries and projects such as log4j, apache-commons etc.

I select all the projects in Eclipse and export as a JAR file, then copy the JAR file into my /WEB-INF/lib folder of my WAR, then deploy my application. I have the following problems:

  1. ClassNotFoundException. The web application cannot find the libraries packaged into my JAR file. I solved this problem by moving out all the dependent libraries into the /WEB-INF/lib folder and adding class-path entries into the MANIFEST.MF of the JAR, a thoroughly painful process.

  2. The classes in the JAR file cannot find property files packaged inside the JAR. How do I solve this problem?

Is there a standard solution for these problems? Thanks.


回答1:


I would recommend using something that does automatic dependency management for you. If you already have ant in place for your build script, ivy is a great solution. If you don't have a build script in place, maven might be worth looking into.

Dependency management automatically pulls in transitive dependencies for dependencies (jars) that you use directly.




回答2:


The standard solution to this problem is putting all jar files you depend on in the WEB-INF/lib folder. What you are looking for is a non-standard solution.

Of those, there are any number. Modifying classpath in the manifest file is one. You could also put them in the container shared library directory or put them anywhere and add them to the global classpath of the container process. All would work but none are considered best-practice.




回答3:


I had the same problem with the deployment. To solve I just changed the properties of the project:

  • Right Click on the project
  • Select "properties" --> "Deployment Assembly" -->"add" --> "Java Build Path Entries"
  • Select all the libraries that you want to add to your web app

If done correctly this'll automatically add the external .jar library to your .war file.



来源:https://stackoverflow.com/questions/6897843/packaging-jar-file-in-war-file

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