Executable .jar, with onejar-maven-plugin

Onejar-maven-plugin collects your application including dependencies, into one executable jar. It’s both easy and works well!

(UPDATED for version 1.3.0. See below.)

Problem

If you have ever tried doing this before with Maven, you have probably used maven-assembly-plugin, which would leave the door open for classpath problems!

It would extract all your dependency jars in one directory together with all your class files and all other classpath resources. The problem is that everything ended up in a big mix, with classpath resources possibly overwriting each other. For example, if two dependencies each had a log4j.properties file in their jar, one log4j.properties would overwrite the other.

No more of that mess!

Solution

Enter maven-onejar-plugin. It lets all your dependency jars stay jars, and your code is in its own jar. All of those jars are put in a bigger jar, which is made executable.

Configuration

It may sound weird, but it’s quite elegant! Just put this in your pom.xml‘s <plugins> tag to make it work:

You also need to add this to the pom:

Please note the new Maven repository URL. Me and onejar-maven-plugin founder Drew Stovall just recently moved the project to Google Code to make it more open and easier for others (like me!) to pitch in. Yesterday, I released version 1.2.3 of onejar-maven-plugin to our new Maven repo at Google Code. UPDATE: Just released version 1.3.0 with optional support for including and autoloading native libraries such as .dll files. (detailed usage instructions.)

Make sure you get the <pluginRepository> URL right so that the latest version will be available to you!

Use

Then do this to build everything:

That will build both your normal jar, as well as another jar. You will get these:

Standalone one-jar.jar file, along with the original .jar file.

Standalone one-jar.jar file, along with the original .jar file.

myApp.one-jar.jar is the big executable that includes both myApp.jar, and all dependency jars.

You can run it standalone without any extra files, like this:

If you found this interesting, you might find my post about winstone-maven-plugin enlightning as well. It shows you how to make an executable .war file.

Credits

Thank you Drew Stovall for creating this Maven plugin in the first place, and thank you to the other contributors who have also submitted patches.

Project homepage: http://onejar-maven-plugin.googlecode.com

21 Comments

  1. Ulrik Sandberg

    If you run into problems when using onejar-maven-plugin in an annotation-based Spring project, chances are that it’s the classpath scanning that is the problem.

    I had the following setup:

    Class:

    Spring config:

    Bootstrapping code:

    I got the following exception:

    I then switched to this config, still using annotation-based dependency autowire, but requiring an explicit bean definition:

    Spring config:

  2. Elisha

    Hi, I tried using the one-jar plugin with maven 2 but I keep on getting the following error message when I run mv install:

    Error message: Failed to resolve plugin for mojo binding: org.dstovall:onejar-maven-plugin:1.4.1:one-jar
    Root error message: Unable to download the artifact from any repository

    I added everything I should have using the example above. I don’t understand how I’m getting this error. Please help asap.

    • Hugo Josefson

      @Elisha, thanks for your feedback!

      I tried the same thing too, and it Works For Me (TM).

      Please double-check the <pluginRepositories> and <pluginRepository> tags. If the error persists, please open an Issue at the onejar-maven-plugin project page, so we can help track down the problem. It will then help if you can attach your pom.xml to the issue.

      Project issue tracker: http://code.google.com/p/onejar-maven-plugin/issues/list

      /Hugo

  3. Elisha

    Hi Hugo,

    Everything seems to be working now. It seems that it was unable to add the plugin repository because I was accessing my repository on another machine. It is now fine. I seem to be having another issue though when I try and execute the jar. I will open an issue on this one at the onejar-maven-plugin project page.

    Elisha

  4. Jon

    I just put in a defect here http://code.google.com/p/onejar-maven-plugin/issues/list. Having a problem with the one-jar repository id and url with Nexus.

  5. Stevo Slavic

    Just to confirm on what Ulrik wrote, component scanning doesn’t work with onejar. Not sure yet whom to blame.

  6. Kev

    Another confirmation that Spring component scanning doesn’t work with onejar. Bummer. Confirmation of Ulrik and Stevo’s findings.

  7. Sebastian

    I provided a fix for the Spring component scanning problem at http://code.google.com/p/onejar-maven-plugin/issues/detail?id=12

  8. David

    Hi.

    I need your help.

    I have to include manually a jar-file into the one-jar file. I need to put this jar-file in the lib folder. How can I do it? The jar-file is an indirect dependency and the pluggin doesn’t include it automatically

    Thanks.

  9. David

    I want to do something like this:

    ${project.build.directory}/tmp/lib

    tools.jar

    the only thing that I want to change is the destiny folder into de one-jar file. I want to send “tolls.jar” into /lib. Exists any tag in the plugin to do this?

    Thanks.

  10. David

    Sorry for the last message…I want to write it:

    ${project.build.directory}/tmp/lib

    tools.jar

  11. David

    I’m sorry again. I’m fighting with this message…

    binlibs
    fileSet
    directory
    ${project.build.directory}/tmp/lib
    /directory
    includes
    include
    tools.jar
    /include
    /includes
    /fileSet
    /binlibs

  12. Susan

    I’m having trouble building. I get the error Unrecognised tag: ‘executions’ (position: START_TAG seen …rn

  13. Dawid

    Thanks Hugo for this post and the plugin. Great job !

  14. Lauri

    What if I don’t want the ‘one-jar’ in the jar name? Is there a way to define the jar name?

  15. Tonio

    Great wonderful, really love this for utility projects awsome.
    thanks a lot.

  16. JG

    Hi,
    I am using one jar plugin and I am able to get the jar compiled. However, the jars are nested. for example, I am trying to use org.acceleo.engine jar.

    The jar is inside the lib folder and inside the jar again I have the jar.
    During runtime, the application complains that it cannot find org.acceleo.engine.IModel class.

    I have a bunch of such jars to be included. Can you help me resolve this nested jar packaging issue.

    Best Regards,
    Jyothi

  17. So cool!!! Thanks!

  18. Trishala

    I tried using above method to create executable with maven+testng and I am getting error – Exception in the thread “main” java.lang.class.exception.

  19. The MANIFEST.MF file can not handle long path names as in this example:
    Manifest-Version: 1.0
    ImplementationVersion: 1.0
    Main-Class: com.simontuffs.onejar.Boot
    One-Jar-Main-Class: nl.verheulconsultants.switchispmaven.SwitchISPServ
    ice

    A CRLF is inserted in SwitchISPService which caused a problem for me.

Leave a Reply