DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Validate Names Using Java
  • DDD and Spring Boot Multi-Module Maven Project
  • Maven Dependency Scope Applied
  • A Maven Story

Trending

  • Key Considerations in Cross-Model Migration
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • How to Format Articles for DZone
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Coding
  3. Java
  4. Creating Executable Uber Jar’s and Native Applications with Java 8 and Maven

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

By 
Andy Moncsek user avatar
Andy Moncsek
·
Oct. 12, 14 · Interview
Likes (0)
Comment
Save
Tweet
Share
29.1K Views

Join the DZone community and get the full member experience.

Join For Free

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

Executable JAR (file format) application Java (programming language) Apache Maven Dependency

Opinions expressed by DZone contributors are their own.

Related

  • How To Validate Names Using Java
  • DDD and Spring Boot Multi-Module Maven Project
  • Maven Dependency Scope Applied
  • A Maven Story

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!