Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Building an executable jar with Maven

Building an executable jar with Maven

December 23rd, 2010 Vinay Leave a comment Go to comments

This is one of the most commonly asked questions on Maven.

How to build executable jar with maven.

The idea is to build a jar with all its dependencies so that you can execute java – <nameof jarfile.jar>

Here is how to do it in maven. Add this to your pom.xml

<build>
<plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

You can run mvn assembly:assembly and a jar file with all the dependencies will be built.

This is good if this is a standalone project. But if you have a parent pom and there is sub project which needs an executable jar, use goal single to build it along with the rest of the application.

Here is the pom.xml for this

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fully.qualified.nameOfCLass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Now if you run as mvn single package or just mvn, it will build the executable jar along with the rest of the project.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • Google Bookmarks
  • IndianPad
  • Reddit
Categories: Programming / tutorials Tags:
  1. January 3rd, 2011 at 07:42 | #1

    Thanks Samarth. We did not face this problem but I am sure some of the readers might face this issue.

  2. samarth
    January 2nd, 2011 at 23:11 | #2

    But there is problem when you use overlapping resources(like .properties e.g. spring.handlers or spring.schemas). Then you need to append them rather than overwriting. This can be achieved using shade. To see how it works http://xebee.xebia.in/2010/12/31/create-an-executable-jar-using-maven-shade-plugin/

  3. December 29th, 2010 at 14:17 | #3

    The idea is that the executable jar would be run independent of main package. For eg if you have a project which builds .war file, we are building a jar core.jar which has core classes. Now I also want to create a core.jar with all the dependencies because I want to run on command prompt a main class.
    When I am packaging as .war I will include core.jar and exclude core-with-dependencies.jar

  4. uis67et9
    December 29th, 2010 at 13:59 | #4

    But will not this kind of mashed-together jars give you e.g. resource conflicts?
    How do you control the order that dependencies are loaded if they are put in the same jar?

    One scenario is when your application overrides a resource file from one of the dependencies. This works if your classpath includes your jars/classes before the deps. But when they are located in the same jar, in the same path, with the same name …?

  5. December 27th, 2010 at 10:13 | #5
  6. December 27th, 2010 at 01:16 | #6

    There’s also a Shade plugin that builds up an executable uberjar.

  1. December 24th, 2010 at 00:42 | #1
  2. December 24th, 2010 at 19:41 | #2
Get Adobe Flash playerPlugin by wpburn.com wordpress themes