Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming a fat jar with Maven

When I create a jar file I want to fit inside my dependencies. For that, I use maven-assembly-plugin such as follows:

<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. I would like to eliminate that "jar-with-dependencies" ending. 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

After the solution the user Tunaki suggested, I used a different pluggin and maven works as I want it to do it. 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>
like image 230
Rors Avatar asked Jan 09 '16 15:01

Rors


1 Answers

First, you need to understand why you are getting this warning.

The Maven convention is that one project should create a single main artifact. For a project of packaging jar, the main artifact is the result of the maven-jar-plugin. This plugin will package as a JAR the classes contained in your project only.

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. 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. To distinguish this secondary additional artifact from the main one, the classifier jar-with-dependencies is added.

So when you remove the classifier, you effectively replace the main artifact with the uber-jar. maven-assembly-plugin will emit a warning in this case, and that's the warning you are having. 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:

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.

like image 143
Tunaki Avatar answered Sep 30 '22 18:09

Tunaki