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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Testing Applications With JPA Buddy and Testcontainers
  • Conditional Breakpoints: A Guide to Effective Debugging
  • Observability Architecture: Financial Payments Introduction
  • Fun Is the Glue That Makes Everything Stick, Also the OCP
  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

Andy Moncsek user avatar by
Andy Moncsek
·
Oct. 12, 14 · Interview
Like (0)
Save
Tweet
Share
28.22K 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.

Trending

  • Testing Applications With JPA Buddy and Testcontainers
  • Conditional Breakpoints: A Guide to Effective Debugging
  • Observability Architecture: Financial Payments Introduction
  • Fun Is the Glue That Makes Everything Stick, Also the OCP

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: