如何使用 Maven 将目录添加到 JAR 文件?
我正在尝试使用 Maven 将 Web 应用程序打包为可运行的 JAR 文件。 jar-with-dependencies 程序集负责包含所有依赖项,但我仍然需要包含src/main/webapp。
我设法使用自定义程序集将该目录添加到我的 JAR 中:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>webapp</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/webapp</directory>
<outputDirectory>/webapp</outputDirectory>
</fileSet>
</fileSets>
</assembly>
它确实有效,甚至可以将此程序集与 jar-with-dependencies 一起使用。但是,最终的 JAR 包含 webapp 目录和所有依赖项,但不包含我的项目的类文件。我的大会显然正在移除它们。
我可以在程序集中保留我自己项目的类文件吗?或者还有其他方法将目录添加到 JAR 中吗?
I'm trying to package a web application as a runnable JAR file with Maven. The jar-with-dependencies assembly takes care of including all the dependencies, but I still need to include src/main/webapp.
I managed to add the directory to my JAR with a custom assembly:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2
http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>webapp</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/webapp</directory>
<outputDirectory>/webapp</outputDirectory>
</fileSet>
</fileSets>
</assembly>
And it does work, and it's even possible to use this assembly together with jar-with-dependencies. However, the final JAR contains the webapp directory and all dependencies, but not the class files of my project. My assembly is apparently removing them.
Can I preserve the class files of my own project in an assembly? Or is there another way to add a directory to a JAR?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜这是相反的,它没有添加它们。
您的类文件位于 target/classes 内部,并且它们需要进入 target/webapp/WEB-INF/classes 内部。我猜你需要另一个这样的规则:
I'm guessing it's the other way around, it's not adding them.
Your class files are inside target/classes and they need to go inside target/webapp/WEB-INF/classes. I'm guessing you need another rule like this:
您的类将在 ${project.build.directory}/classes (可能是/target/classes)中生成。
因此,您应该使用该文件夹作为源目录。
尝试将{project.build.directory}/classes
src/main/webapp
更改为You classes will be generated in ${project.build.directory}/classes (/target/classes probably).
Therefore, you should use that folder as source directory.
Try to change
<directory>src/main/webapp</directory>
into<directory>{project.build.directory}/classes</directory>
或者,您可以在程序集描述符中添加以下内容。假设您的 Maven 项目正在构建一个 Web 应用程序,这不仅会处理类文件的放置,还会处理 Web 应用程序的内容。
Alternately, you could add the following in your assembly descriptor. This would take care of not only putting the class files, but also the webapp contents, assuming your maven project is building a webapp.