简体   繁体   English

用Maven重命名一个胖罐子

[英]Renaming a fat jar with Maven

When I create a jar file I want to fit inside my dependencies. 当我创建一个jar文件时,我想要适应我的依赖项。 For that, I use maven-assembly-plugin such as follows: 为此,我使用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>

This code works OK and it does what it's expected to do. 这段代码工作正常,它可以完成预期的工作。 However, this creates a new jar called myjar-GUI-jar-with-dependencies.jar . 但是,这会创建一个名为myjar-GUI-jar-with-dependencies.jar的新jar。 I would like to eliminate that "jar-with-dependencies" ending. 我想消除那个“jar-with-dependencies”的结局。 Does anybody knows how to do that? 有谁知道怎么做?

I have used that commented line you can see on my code, but that produces the following warning that I don't know how to solve it: 我已经使用了你可以在我的代码上看到的注释行,但是这会产生以下警告:我不知道如何解决它:

[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

EDITED EDITED

After the solution the user Tunaki suggested, I used a different pluggin and maven works as I want it to do it. 在用户Tunaki建议的解决方案之后,我使用了不同的插件和maven,因为我希望它能够实现。 The code is as follows: 代码如下:

            <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>

First, you need to understand why you are getting this warning. 首先,您需要了解为什么会收到此警告。

The Maven convention is that one project should create a single main artifact. Maven的惯例是,一个项目应建立一个单一的主神器。 For a project of packaging jar , the main artifact is the result of the maven-jar-plugin . 对于包装jar的项目,主要工件是maven-jar-plugin This plugin will package as a JAR the classes contained in your project only. 此插件仅将JAR包含在项目中包含的类中。

One project can eventually generate additional artifacts that will be distinguished from the main one by their classifier : 一个项目最终可以生成其他工件,这些工件将通过其分类器与主工件区分开来:

Beside the main artifact there can be additional files which are attached to the Maven project. 除了主要工件之外,还可以附加到Maven项目的其他文件。 Such attached filed can be recognized and accessed by their classifier. 这种附加的字段可以由其分类器识别和访问。

The classifier is an identifier than will be appended to the main artifact name. 分类器是将附加到主工件名称的标识符。

So what happens when you want to create an uber-jar? 那么当你想创造一个超级罐时会发生什么? Somehow, your project needs to generate two jars. 不知何故,您的项目需要生成两个罐子。 The main one will be a JAR containing the classes of your project and the second one will be the uber-jar resulting of maven-assembly-plugin . 主要的是一个包含项目类的JAR,第二个是由maven-assembly-plugin生成的超级jar。 To distinguish this secondary additional artifact from the main one, the classifier jar-with-dependencies is added. 为了将该辅助附加工件与主工件区分开来,添加了分类器jar-with-dependencies

So when you remove the classifier, you effectively replace the main artifact with the uber-jar. 因此,当您删除分类器时,您可以使用uber-jar有效地替换主工件。 maven-assembly-plugin will emit a warning in this case, and that's the warning you are having. 在这种情况下, maven-assembly-plugin会发出警告,这就是你的警告。 You can ignore it completely: it just reminds you that you are replacing the main artifact of the project by an additional artifact. 您可以完全忽略它:它只是提醒您,您正在通过其他工件替换项目的主要工件。


Besides the maven-assembly-plugin , do note that you can also generate an uber-jar with the maven-shade-plugin : 除了maven-assembly-plugin ,请注意你还可以使用maven-shade-plugin生成一个超级jar:

This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - ie rename - the packages of some of the dependencies. 这个插件提供了将工件打包在超级jar中的功能,包括它的依赖关系和阴影 - 即重命名 - 一些依赖项的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM