为什么Maven的目标是“打包”?包括jar中的资源,但目标是“jar:jar”。不? [英] Why does the Maven goal "package" include the resources in the jar, but the goal "jar:jar" doesn't?
问题描述
当我使用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文件也不包含我的资源。就我而言,它应该使用与包目标相同的目录(在我的情况下继承自Maven Super POM)。
jar:jar
目标 =nofollow noreferrer> Maven JAR插件用于从当前项目构建JAR 并且只做一件事:它打包目标的内容/ classes
进入目标
目录中的JAR,就是这样。所以,当你运行 mvn jar:jar
时,你的pom 中的插件配置是,但是 jar:jar
不会做比我提到的更多的事情。如果 target / classes
为空或不存在,则不会在生成的JAR中打包任何类或资源。
包
阶段是构建生命周期阶段,当你调用 mvn package
时,之前的所有阶段将执行 package
( process-resources
, compile
, process-test-resources
等)并将触发插件目标绑定到这些阶段。因此,对于< packaging>
类型为 jar
的项目, jar:jar
绑定到包
并将在包
阶段运行,但在此之前,将触发绑定到 package
之前的阶段的目标,包括将资源复制到 target / classes
中的目标。 / p>
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).
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 of target/classes
into a JAR in the target
directory, and that's all. So, when you run mvn jar:jar
, the plugin configuration in your pom is used, but jar:jar
won't do more things than what I mentioned. If target/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 invoke mvn package
, all phases before package
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 type jar
, jar:jar
is bound to package
and will be run during the package
phase but prior to that, the goals bound to phases preceding package
will be triggered, including the one that copies the resources into target/classes
.
这篇关于为什么Maven的目标是“打包”?包括jar中的资源,但目标是“jar:jar”。不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!