用 Maven 重命名一个 fat jar

Renaming a fat jar with Maven

当我创建一个 jar 文件时,我想将其放入我的依赖项中。为此,我使用 maven-assembly-plugin 如下:

<build>
    ...
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <finalName>${project.artifactId}-GUI</finalName>
                <archive>
                    <manifest>
                        <mainClass>gui.MyMainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <!-- <appendAssemblyId>false</appendAssemblyId>-->
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
 </build>

这段代码工作正常,它完成了预期的工作。但是,这会创建一个名为 myjar-GUI-jar-with-dependencies.jar 的新 jar。我想消除 "jar-with-dependencies" 结尾。有人知道怎么做吗?

我已经使用了您可以在我的代码中看到的注释行,但是会产生以下警告,我不知道如何解决它:

[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: [myJar-GUI].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!
[WARNING] Replacing pre-existing project main-artifact file: [myJar].jar with assembly file: [myJar-GUI].jar

已编辑

在用户 Tunaki 建议的解决方案之后,我使用了不同的插件,并且 maven 可以按照我希望的方式工作。代码如下:

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <transformers>
                    <transformer
                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>gui.SparkISGUI</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </plugin>

首先,您需要了解收到此警告的原因。

Maven 惯例是一个项目应该创建一个单个 主工件。对于打包项目 jar,主要工件是 maven-jar-plugin 的结果。此插件将仅将 类 包含在您的项目中的 JAR 打包。

一个项目最终可以生成额外的工件,这些工件将通过 classifier:

与主要工件区分开来

Beside the main artifact there can be additional files which are attached to the Maven project. Such attached filed can be recognized and accessed by their classifier.

分类器是一个标识符,将附加到主要工件名称。

那么当你想创建一个 uber-jar 时会发生什么?不知何故,您的项目需要生成 两个 罐子。主要的是一个包含项目 类 的 JAR,第二个是 maven-assembly-plugin 的结果 uber-jar。为了将这个次要附加工件与主要工件区分开来,添加了分类器 jar-with-dependencies

因此,当您删除分类器时,您实际上用 uber-jar 替换了主要工件。 maven-assembly-plugin 在这种情况下会发出警告,这就是您收到的警告。你可以完全忽略它:它只是提醒你你正在用一个额外的工件替换项目的主要工件。


除了 maven-assembly-plugin,请注意您还可以使用 maven-shade-plugin:

生成 uber-jar

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.