java 将 JAR 文件打包成 WAR 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6897843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Packaging JAR file in WAR file
提问by Justin Wong
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.
我有一系列依赖的 Java 项目。我想将它们打包成一个单独的 JAR 文件,以便在我的 WAR 文件中使用。这些项目依赖于大量的外部库和项目,如 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. I have the following problems:
我在 Eclipse 中选择所有项目并导出为 JAR 文件,然后将 JAR 文件复制到我的 WAR 的 /WEB-INF/lib 文件夹中,然后部署我的应用程序。我有以下问题:
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?
类未找到异常。Web 应用程序找不到打包到我的 JAR 文件中的库。我通过将所有依赖库移出到 /WEB-INF/lib 文件夹并将类路径条目添加到 JAR 的 MANIFEST.MF 中解决了这个问题,这是一个非常痛苦的过程。
JAR 文件中的类找不到打包在 JAR 中的属性文件。我该如何解决这个问题?
Is there a standard solution for these problems? Thanks.
这些问题有标准的解决方案吗?谢谢。
回答by Dave
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.
我会建议使用一些为你做自动依赖管理的东西。如果您已经为构建脚本安装了 ant,ivy 是一个很好的解决方案。如果您没有适当的构建脚本,maven 可能值得研究。
Dependency management automatically pulls in transitive dependencies for dependencies (jars) that you use directly.
依赖管理会自动为您直接使用的依赖(jar)引入传递依赖。
回答by pap
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.
此问题的标准解决方案是将您依赖的所有 jar 文件放在 WEB-INF/lib 文件夹中。您正在寻找的是非标准解决方案。
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.
其中,有任何数量。修改清单文件中的类路径就是其中之一。您也可以将它们放在容器共享库目录中或将它们放在任何地方并将它们添加到容器进程的全局类路径中。所有这些都可以工作,但没有一个被认为是最佳实践。
回答by Achraf Mounir
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
- 右键单击项目
- 选择“属性”-->“部署程序集”-->“添加”-->“Java 构建路径条目”
- 选择要添加到 Web 应用程序的所有库
If done correctly this'll automatically add the external .jar library to your .war file.
如果操作正确,这将自动将外部 .jar 库添加到您的 .war 文件中。