Over a million developers have joined DZone.

Creating Executable Uber Jar’s and Native Applications with Java 8 and Maven

· DevOps Zone

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

Creating uber jar’s in Java is nothing particular new, even creating executable jar’s was possible with maven long before Java 8.

With the first release of JavaFX 2, Oracle introduced the javafxpackager tool, which has now been renamed to javapackager (Java 8 u20). This enables developers to create native executables for any common platform, even mac app store packages; the drawbacks of the javapackager are that you must create the executable on the target platform and that it will than contain the whole jre to run the application. This means that your 100kb application gets more than 40mb, depending on your target platform. 

The first step on your way to an executable uber jar is to build your project and to collect all dependencies in a folder or a fat jar. This folder/fat jar will be the input for the javapackager tool, which creates the executable part.

Solution 1: the maven-dependency-plugin

The maven-dependency-plugin contains the goal “unpack-dependencies”. We can use this goal to enhance the default “target/classes” folder with all the dependencies you need to run your application. We assume that the default maven build creates all classes of your project in the “target/classes” folder and the “unpack-dependencies” plugin copies all the project dependencies to this folder. The result is a valid input for the javapackager tool, which creates the executable. 

The “unpack-dependencies” goal is easy to use; you just need to set the output directory to the “target/classes” folder to ensure everything is together in one folder. A typical configuration looks like this:

 
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <excludeScope>system</excludeScope>
                <excludeGroupIds>
                    org.springframework.jmx
                </excludeGroupIds>
                <outputDirectory>
                    ${project.build.directory}/classes
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

To create an executable jar from the target/classes directory we use the “exec-maven-plugin” to execute the javapackager commandline tool.

 
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>package-jar</id>
            <phase>package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
            <configuration>
                <executable>
                    ${env.JAVA_HOME}/bin/javapackager
                </executable>
                <arguments>
                    <argument>-createjar</argument>
                    <argument>-appclass</argument>
                    <argument>${app.main.class}</argument>
                    <argument>-srcdir</argument>
                    <argument>
                        ${project.build.directory}/classes
                    </argument>
                    <argument>-outdir</argument>
                    <argument>./target</argument>
                    <argument>-outfile</argument>
                    <argument>
                        ${project.artifactId}-app
                    </argument>
                    <argument>-v</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

Now we can create an executable jar, which contains all the project dependencies and that can simply be executed with “java –jar myApp.jar”.  

In the next step we want to create a native executable or an installer. Specific configuration details for the javapackager can be found here: http://docs.oracle.com/javase/8/docs/technotes/tools/unix/javapackager.html , for this tutorial we assume that we want to create a native installer. To do so, we add a second “execution” to your “exec-maven-plugin” like this:

<execution>
    <id>package-jar2</id>
    <phase>package</phase>
    <goals>
        <goal>exec</goal>
    </goals>
    <configuration>
        <executable>
            ${env.JAVA_HOME}/bin/javapackager
        </executable>
 
        <arguments>
            <argument>-deploy</argument>
            <argument>-native</argument>
            <argument>installer</argument>
            <argument>-appclass</argument>
            <argument>${app.main.class}</argument>
            <argument>-srcfiles</argument>
            <argument>
                ${project.build.directory}/${artifactId}-app.jar
            </argument>
            <argument>-outdir</argument>
            <argument>./target</argument>
            <argument>-outfile</argument>
            <argument>
                ${project.artifactId}-app
            </argument>
            <argument>-v</argument>
        </arguments>
    </configuration>
</execution>

Once the configuration is done, you can run “mvn clean package” and you will find your executable jar as well as the native installer in your target folder.
This solution works fine in most cases, but sometimes you can get in trouble with this plugin. When you have configuration files in your project that also exist in one of your dependencies, the unpack-dependency goal will overwrite your configuration file. For example your project contains a file like “META-INF/service/com.myconf.File” and any dependency does contain the same file. In this case solution 2 may be a better approach.

Solution 2: the maven-shade plugin

The maven-shade plugin provides the capability to package artifacts into an uber-jar, including its dependencies. It also provides various transformers to merge configuration files or to define the MANIFEST of your jar file. This capability allows us to create an executable uber jar without involving the javapackager, so the packager is only needed to create the native executable.

To build an executable uber jar following plugin configuration is needed:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <excludes>
                        <exclude>junit:junit</exclude>
                        <exclude>jmock:*</exclude>
                        <exclude>*:xml-apis</exclude>
                        …
                    </excludes>
                </artifactSet>
                <transformers>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                        <resource>
                            META-INF/services/conf.file
                        </resource>
                    </transformer>
                    <transformer
                            implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <manifestEntries>
                            <Main-Class>${app.main.class}</Main-Class>
                            <X-Compile-Source-JDK>${maven.compile.java.version}</X-Compile-Source-JDK>
                            <X-Compile-Target-JDK>${maven.compile.java.version}</X-Compile-Target-JDK>
                        </manifestEntries>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin> 

This configuration defines a main-class entry in the MANIFEST and merges all “META-INF/services/conf.files” together. The resulting executable jar file is now a valid input for the javapacker to create a native installer. The configuration to create a native installer with “exec-maven-plugin” and javapacker tool is exactly the same like in solution 1.

I provided two example projects on github https://github.com/amoAHCP/mvnDemos where you can test both configurations. These are two simple projects with a main class starting a jetty webserver on port 8080, so the only dependency is jetty. Both solutions can be used for any type of java projects, even with Swing or JavaFX.

Reference:

http://jacpfx.org/2014/10/08/uber-jars.html

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}