为什么 Maven 目标“打包”?包括 jar 中的资源,但目标“jar:jar”是不是吗?
当我使用 Maven 目标“package”打包我的项目时,资源也包含在内。它们最初位于目录“src/main/resources”中。 因为我想创建一个可执行 jar 并将类路径添加到清单中,所以我使用 maven-jar-plugin。
我已将其配置如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>at.program.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
为什么使用“jar:jar”创建的 jar 文件也不包含我的资源。就我而言,它应该使用与“package”目标相同的目录(在我的例子中是从 Maven Super POM 继承的)。
when I package my project with the Maven goal "package", the resources are included as well. They are originally located in the directory "src/main/resources".
Because I want to create an executable jar and add the classpath to the manifest, I'm using maven-jar-plugin.
I've configured it as the following likes:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>at.program.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Why won't the jar file created with "jar:jar" include my resources as well. As far as I'm concerned it should use the same directories as the "package" goal (which are in my case inherited from the Maven Super POM).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jar:jar
Maven JAR 插件 的目标是用于“从当前项目构建 JAR”,并且只执行一件事:将target/classes
的内容打包到target
中的 JAR 中code> 目录,仅此而已。因此,当您运行mvn jar:jar
时,会使用 pom 中的插件配置,但jar:jar
不会做更多事情比我提到的。如果target/classes
为空或不存在,则生成的 JAR 中不会打包任何类或资源。package
阶段是一个构建生命周期阶段,当您调用mvn package
时,之前的所有阶段package
将被执行(process-resources
、compile
、process-test-resources
等)并将触发插件目标 绑定到这些阶段。因此,对于
类型为jar
的项目,jar:jar
绑定到package
并将在package
阶段运行,但在此之前,将触发与package
之前的阶段绑定的目标,包括将资源复制到target 的目标/类
。The
jar:jar
goal of the Maven JAR plugin is used to "build a JAR from the current project" and does only one thing: it packages the content oftarget/classes
into a JAR in thetarget
directory, and that's all. So, when you runmvn jar:jar
, the plugin configuration in your pom is used, butjar:jar
won't do more things than what I mentioned. Iftarget/classes
is empty or doesn't exist, no classes or resources will be packaged in the resulting JAR.The
package
phase is a build lifecycle phase and when you invokemvn package
, all phases beforepackage
will be executed (process-resources
,compile
,process-test-resources
, etc) and will trigger the plugin goals bound to these phases. So, for a project with a<packaging>
of typejar
,jar:jar
is bound topackage
and will be run during thepackage
phase but prior to that, the goals bound to phases precedingpackage
will be triggered, including the one that copies the resources intotarget/classes
.您可以尝试使用 maven.jar.includes你的jar插件配置。
You can try using maven.jar.includes in you jar plugin configuration.
正如我发现的,当项目打包设置为“jar”时,maven-jar-plugin 会自动将自身绑定到包目标。因此,“包”目标可以完成我想要的一切,包括将类路径附加到清单并设置主类。
As I've found out, the maven-jar-plugin binds itself automatically to the package goal when the project packaging is set to "jar". So the "package" goal does everything that I want, including appending the classpath to the manifest and setting a main class.