为什么 Maven 目标“打包”?包括 jar 中的资源,但目标“jar:jar”是不是吗?

发布于 2024-08-31 15:04:34 字数 753 浏览 3 评论 0原文

当我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉城已无爱 2024-09-07 15:04:34

jar:jar Maven JAR 插件目标是用于“从当前项目构建 JAR”,并且只执行一件事:将 target/classes 的内容打包到 target 中的 JAR 中code> 目录,仅此而已。因此,当您运行 mvn jar:jar 时,会使用 pom 中的插件配置,但 jar:jar 不会做更多事情比我提到的。如果 target/classes 为空或不存在,则生成的 JAR 中不会打包任何类或资源。

package 阶段是一个构建生命周期阶段,当您调用mvn package时,之前的所有阶段 package将被执行(process-resourcescompileprocess-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 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.

删除→记忆 2024-09-07 15:04:34

您可以尝试使用 maven.jar.includes你的jar插件配置。

You can try using maven.jar.includes in you jar plugin configuration.

静赏你的温柔 2024-09-07 15:04:34

正如我发现的,当项目打包设置为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文