Thomas Sundberg

March 5, 2011

Create an executable jar from Maven

Filed under: Java, Maven — Tags: , , , , — Thomas Sundberg @ 17:54

Creating an executable jar from Maven is not really hard.

This example will show you how you can include stuff in a jar and make it executable while not including
stuff that is needed only during the build.

The job is done in the plugin maven-assembly-plugin that will include all resources that
is available in the runtime classpath generated by Maven.

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>se.sigma.educational</groupId>
    <artifactId>executable-jar</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>Executable jar example</name>
    <description>An example of an executable jar that includes one dependency and doesn't include another dependency
    </description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <finalName>executable-example</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <executions>
                    <execution>
                        <id>package-jar-with-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <archive>
                                <manifest>
                                    <mainClass>se.sigma.educational.Main</mainClass>
                                </manifest>
                            </archive>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

The depenency commons-math will be included in the final distribution. The dependency
junit will not be included, it’s scope is set to test.

The main method that will be executed is implemented in se.sigma.educational.Main

To run it, execute java -jar executable-example.jar

Resources

About these ads

11 Comments »

  1. Thanks for the good info, however, I still get many exceptions with a spring dependency I’m using. I’ve seen other forms use the maven-jar-plugin and/or the maven-shade-plugin. Do you need more than just the assembly plugin?

    Comment by Daniel — May 12, 2011 @ 17:38

    • I have created executable jars with a setup like this without problems and exceptions. The exceptions you encounter may be caused by other things than the assembly plugin.

      Comment by Thomas Sundberg — May 12, 2011 @ 21:36

  2. Hey, at last I found something that works!!! I have been all the day searching but nothing seemed to work until I found your blog.
    Thank you very much, mate! You have helped me a lot :-)

    Comment by David — July 17, 2011 @ 15:29

  3. Thanks Thomas for this perfect article!!! Appreciate your work!!

    Comment by Anton — October 21, 2012 @ 18:55

  4. Hi Thomas,
    Thanks for the clearly written article. I wonder if you have ever had to deal with a problem where common top-level resources like log4j.properties would be overwritten by the various other jars that contain it? If so, how did you cope with that?

    Comment by Brendan — November 8, 2012 @ 18:02

    • Hi!

      I don’t think I have experienced that. If I did, I would explore the Maven capability to exclude parts of a dependency. This may not be enough or easy, but this is where I would start.

      I am glad that you found my blog to be clear and easy to read. It is one of my goals, to write things that are easy to access and understand.

      /Thomas

      Comment by Thomas Sundberg — November 8, 2012 @ 20:46

      • In the end I just changed my main to pick up an explicitly named properties file in an non-standard directory, instead of expecting my log4j.properties file to be picked out of the classpath in preference to other identically-named files.

        Comment by Brendan — November 26, 2012 @ 13:41

  5. Thx for your post, it helped me,

    Comment by sadiq81 — August 22, 2013 @ 21:48

  6. Thanks for posting. Was really helpful

    Comment by Mohan — August 19, 2014 @ 13:46


RSS feed for comments on this post. TrackBack URI

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

The Silver is the New Black Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 56 other followers

%d bloggers like this: