简体   繁体   English

在WAR文件中打包JAR文件

[英]Packaging JAR file in WAR file

I have a series of dependent Java projects. 我有一系列依赖的Java项目。 I'd like to package them together into a single JAR file to be used in my WAR file. 我想将它们打包成一个JAR文件,以便在我的WAR文件中使用。 These projects depend on a large number of external libraries and projects such as log4j, apache-commons etc. 这些项目依赖于大量的外部库和项目,如log4j,apache-commons等。

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. 我选择Eclipse中的所有项目并导出为JAR文件,然后将JAR文件复制到我的WAR的/ WEB-INF / lib文件夹中,然后部署我的应用程序。 I have the following problems: 我有以下问题:

  1. ClassNotFoundException. ClassNotFoundException异常。 The web application cannot find the libraries packaged into my JAR file. Web应用程序找不到打包到我的JAR文件中的库。 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. 我通过将所有依赖库移出到/ WEB-INF / lib文件夹并将类路径条目添加到JAR的MANIFEST.MF来解决了这个问题,这是一个非常痛苦的过程。

  2. The classes in the JAR file cannot find property files packaged inside the JAR. JAR文件中的类无法找到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. 如果你已经为你的构建脚本安装了ant,那么常春藤是一个很好的解决方案。 If you don't have a build script in place, maven might be worth looking into. 如果您没有构建脚本,maven可能值得研究。

Dependency management automatically pulls in transitive dependencies for dependencies (jars) that you use directly. 依赖关系管理会自动为您直接使用的依赖项(jar)提供传递依赖项。

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" 选择“属性” - >“部署程序集” - >“添加” - >“Java构建路径条目”
  • Select all the libraries that you want to add to your web app 选择要添加到Web应用程序的所有库

If done correctly this'll automatically add the external .jar library to your .war file. 如果正确完成,这将自动将外部.jar库添加到.war文件中。

The standard solution to this problem is putting all jar files you depend on in the WEB-INF/lib folder. 此问题的标准解决方案是将您依赖的所有jar文件放在WEB-INF / lib文件夹中。 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. 一切都会奏效,但没有一个被认为是最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM