Packaging JAR file in WAR file
I have a series of dependent Ja开发者_高级运维va 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:
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.
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.
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.
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.
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.
精彩评论