Create an Executable Jar with Maven

February 18, 2009 at 2:32 pm 3 comments

There are several ways to make an executable jar with Maven.  Two popular ways are to use the maven-jar-plugin or use the maven-assembly-plugin.  Each has pro’s and con’s.

With the maven-jar-plugin, your executable jar won’t include it’s dependencies.  But I want my executable jars to include everything needed to run.  A benefit of the Jar plugin is that, the executable jar is your project’s main artifact and it has the name you expect.  Either ${} or ${}.

Using the maven-assembly-plugin lets you include all dependencies in the executable jar.  But the jar isn’t your project’s main articifact  It also typically requires a separate assembly definition file.  You can use a default descriptorRef “jar-with-dependencies” to avoid that, but then you’re stuck with a final jar name with “-jar-with-dependencies” appended to it.  Another problem is that when the Assembly plugin adds dependencies, it includes all their dependencies too.  This can quickly lead to a gigantic final jar.

I think I’ve found a simpler way to create an all-in-one executable jar.  This method uses the maven-jar-plugin along with the maven-shade-plugin.  The Shade plugin lets you add dependencies.  Fortunately, by default, it only includes your project’s direct dependencies and no transitive dependencies.  (If you need a few transitive dependencies, the Shade plugin lets you.)  As a result, the executable jar is slim and trim.  Another plus, the final executable jar has the name you want and is the project’s primary artifact.

Below is an example of the plugin definitions needed to create an all-in-one executable jar using Maven’s Jar and Shade plugins:


               <!-- Use this to in/exclude only specific dependencies -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer" />


Entry filed under: java, maven, Uncategorized.

Book Reviewlet: Spy Catcher by Peter Wright Amazing Ubuntu 8.10

3 Comments Add your own

  • 1. Sridhar  |  November 5, 2009 at 11:26 am

    Thanks for posting this article. It helped me to create an executable jar file with the dependencies, mentioned in the pom file, unpacked and with the same name as the original artifact.
    Great Plugin!!

  • 2. Shantanu Kumar  |  November 26, 2010 at 2:47 am

    Why not just use the Maven shade plugin?

    • 3. devdanke  |  December 1, 2010 at 11:33 pm

      Thank you for pointing to the shade plugin example of creating an executable jar. It’s looks simpler than my method. If the resulting jar’s name is the project name, then it’s the best way yet. I’ll give it a try to see if it gives the results I want. Thanks again!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


%d bloggers like this: