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 ${project.name} or ${project.build.finalName}.

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:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
     <archive>
       <manifest>
          <mainClass>org.foo.MyMain</mainClass>
       </manifest>
     </archive>
  </configuration>
</plugin>

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>1.2</version>
  <executions>
    <execution>
<phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
         <configuration>
           <artifactSet>
               <!-- Use this to in/exclude only specific dependencies -->
              <includes>
                <include>commons-codec:commons-codec</include>
              </includes>
           </artifactSet>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer" />
           </transformers>
        </configuration>
     </execution>
  </executions>
</plugin>

About these ads

Entry filed under: java, maven, Uncategorized. Tags: .

NetBeans 6.5 Better Than 6.0? Movie Picks

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!!

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

    Why not just use the Maven shade plugin?

    http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html

    Reply
    • 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!

      Reply

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Categories


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: