maven-assembly-plugin:如何使用appendAssemblyId [英] maven-assembly-plugin: How to use appendAssemblyId

查看:2501
本文介绍了maven-assembly-plugin:如何使用appendAssemblyId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多模块Maven项目,在一个模块中我想在构建期间创建两个工件:

I have a multi-module Maven project and in one module I want to create two artifacts during the build:


  • 主要工件这是一个jar库,其他一些模块将依赖它。

  • 执行一些辅助函数的可执行jar文件。没有其他模块依赖于此,它仅供用户在某些情况下手动运行。

这是我的代码用于配置 maven-assembly-plugin 插件:

Here is the code that I use to configure the maven-assembly-plugin plugin:

<plugin>
    <artifactId>
        maven-assembly-plugin
    </artifactId>
    <version>2.4</version>
    <executions>
      <execution>
        <id>dist-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>

        <configuration>
          <finalName>bso</finalName>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <finalName>helper-${project.version}</finalName>
          <appendAssemblyId>false</appendAssemblyId>
          <archive>
            <manifest>
              <mainClass>HelperMain<mainClass>
            </manifest>
          </archive>
        </configuration>

      </execution>
    </executions>
  </plugin>

我将 appendAssemblyId 设置为 false 因为否则 -jar-with-dependencies 将附加到最终名称,我认为不需要它。省略它可以提供更清晰,更易于使用的文件名。

I am setting appendAssemblyId to false because otherwise -jar-with-dependencies will be appended to the final name and I do not see the need for it. Omitting it gives a cleaner and easier to use file name.

当我运行 mvn integration-test 然后我收到以下警告:

When I run mvn integration-test then I receive the following warnings:


[警告]配置选项:'appendAssemblyId'设置为false,并且缺少'classifier'。
而不是附加汇编文件:[...] / target / helper-5.0.0-SNAPSHOT.jar,它将成为主项目工件的文件。

[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing. Instead of attaching the assembly file: [...]/target/helper-5.0.0-SNAPSHOT.jar, it will become the file for main project artifact.

注意:如果为此项目提供了多个描述符或描述符格式,则此文件的值将是不确定的!

NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!

[警告]替换前存在的项目主工件文件:[...] / target / my.module-5.0.0-SNAPSHOT.jar
with assembly file:[...] / target / helper-5.0.0-SNAPSHOT .jar

[WARNING] Replacing pre-existing project main-artifact file: [...]/target/my.module-5.0.0-SNAPSHOT.jar with assembly file: [...]/target/helper-5.0.0-SNAPSHOT.jar

有两件事令我恼火:


  1. 尽管警告声称它将用helper-5.0.0-SNAPSHOT.jar替换my.module-5.0.0-SNAPSHOT.jar,但它实际上并没有这样做,而且构建时有完成两个文件仍然有不同的大小。

  1. Despite the fact that the warning claims it will replace my.module-5.0.0-SNAPSHOT.jar with helper-5.0.0-SNAPSHOT.jar it does not actually do so and when the build has finished both files still have different sizes.

为什么有关更换工件的警告出现?

Why does the warning about replacing the artifact appear at all?

似乎不推荐使用分类器为什么警告会问我们是吗?

It seems that classifier is deprecated why does the warning ask me to use it?


推荐答案

那是因为你错误地解释了这些警告。

That is because you are mis-interpreting the warnings.

让我们回顾一下。默认情况下,不是 pom 类型的Maven项目将始终生成所谓的主工件。对于JAR,此工件是将已编译的源打包到JAR中的结果;对于WAR,它是构建Web应用程序的结果。

Let's recap. A Maven project that is not of type pom will always produce, by default, what is called a main artifact. For a JAR, this artifact is the result of packaging the compiled sources into a JAR; for a WAR, it is the result of building the web-application.

重要的是要记住,这个工件是附加到project:当安装项目(使用 mvn install ),部署(使用 mvn deploy )时,此术语非常有用发布(使用 maven-release-plugin )。 附加表示在项目实施时将安装/部署/发布此工件。并非Maven构建期间生成的所有文件(基本上, target 文件夹下的所有文件)都是;只有附加的文件。因此,您可以在 target 下创建大量文件,但只有一个已安装的工件。

What is important to remember is that this artifact is attached to the project: this terminology is useful when the project is installed (with mvn install), deployed (with mvn deploy) or released (with the maven-release-plugin). Attached means that this artifact will be installed / deployed / released when the project is. Not all files generated during a Maven build (basically, everything under the target folder) are; only the files that were attached. As such, you can very well create a lot of files under target but have a single installed artifact.

旁边这个主要工件,您可能希望构建生成其他工件以进行安装或部署。这是附加或辅助附加工件的概念。主要的例子是Javadoc或源:通常在项目发布时,它的Javadoc及其来源也是。这就是概念分类器开始使用

Alongside this main artifact, you may want your build to produce other artifacts to install or deploy. That's the concept of additional or secondary attached artifacts. The main examples are the Javadoc or the sources: typically when a project is released, its Javadoc and its sources also are. And that is where the notion classifier kicks in.

在Maven存储库中,每个文件都必须遵循相同的命名约定: artifactId的版本(-classifier).TYPE 。每个辅助工件都将具有与主工件相同的GAV(组ID,工件ID,版本),因此如果您想要放入Maven repo 1主工件和1个附加工件(就像主JAR一样)使用它的JAR Javadoc和JAR源代码,你需要一些方法来区分它们。这是分类器的用途:区分辅助工件与主工件。

In a Maven repository, each and every file has to follow the same naming convention: artifactId-version(-classifier).type. Every secondary artifact will have the same GAV (group id, artifact id, version) as the main artifact so if you want to put inside a Maven repo 1 main artifact and 1 attached artifact (like it would be the case for a main JAR along with its JAR Javadoc and JAR sources), you need some way to distinguish them. Which is what the classifier is for: distinguish secondary artifacts from the main artifact.

现在让我们回到你的例子。您的Maven项目是 jar 打包,默认情况下会生成一个名为 my.module-5.0.0-SNAPSHOT.jar的主要JAR工件;仍然默认情况下,此主JAR附加到项目(并准备安装/部署)。现在你正在配置 maven-assembly-plugin 来创建一个新的JAR工件(名为 helper-5.0.0-SNAPSHOT.jar 但真的没关系)。默认情况下,程序集插件将项目附加到项目中它产生了。所以你最终得到2个附加的工件

Let's go back to your example now. Your Maven project, which is of jar packaging, will produce by default a main JAR artifact called my.module-5.0.0-SNAPSHOT.jar; still by default, this main JAR is attached to the project (and ready to be installed / deployed). Now you're configuring the maven-assembly-plugin to create a new JAR artifact (called helper-5.0.0-SNAPSHOT.jar but it really doesn't matter). The Assembly Plugin, by default, attaches to the project the artifact it produces. So you end up with 2 attached artifacts


  1. 具有相同的工件ID my.module ;事实上,目标文件夹中的磁盘上的文件被命名为 helper ,这一事实无关紧要,只有GAV坐标才重要

  2. 具有相同版本的 5.0.0-SNAPSHOT

  3. 具有相同的包装JAR

  1. having the same artifact id of my.module; the fact that the file on disk inside the target folder is named helper for one is irrelevant, only the GAV coordinates matter
  2. having the same version of 5.0.0-SNAPSHOT
  3. having the same packaging of JAR

并且没有分类器来区分它们。这就是提出警告的原因:您最终会在项目中附加一个有效替换主要工件的辅助工件,因为它具有相同的坐标。结果是:

and no classifier to distinguish them. This is what raises the warning: you end up attaching to the project a secondary artifact that effectively replaces the main one, simply because it has the same coordinates. So the result is:


  1. 两个文件在 target 内的磁盘上具有不同的名称,但这是无关紧要的,因为

  2. 两者共享相同的坐标,因此只有1个存活。

  1. Both files having different names on disk inside target, but that is irrelevant because
  2. Both share the same coordinates so only 1 will survive.

它是由Assembly Plugin生成的,它将赢得冲突,并替换附加的主要工件。

It is the one produced by the Assembly Plugin that will win the conflict, and replace the attached main artifact.

如果你想说服自己,请运行<$在项目上c $ c> mvn clean install 并检查您的本地仓库。您会注意到只会安装 jar-with-dependencies 工件。另一个(主要工件)变得很糟糕。

If you want to convince yourself of all that, run mvn clean install on the project and check your local repo. You will notice that only the jar-with-dependencies artifact will be installed. The other one (the main artifact) went poof.

您还可以配置< distributionManagement>

<distributionManagement>
    <repository>
        <id>local-repo-test</id>
        <url>file://...</url>
    </repository>
</distributionManagement>

并调用 mvn clean deploy 。然后,您可以检查唯一部署的工件是 jar-with-dependencies

and invoke mvn clean deploy. You can then check that the only deployed artifact will be the jar-with-dependencies.

最后说明:是的,不推荐使用Assembly Plugin的分类器 参数,因为您应该只使用程序集ID作为分类器。

Final note: Yes, the classifier parameter of the Assembly Plugin is deprecated, because you should just use the assembly id as classifier.

这篇关于maven-assembly-plugin:如何使用appendAssemblyId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆