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 Responses to “Executable .jar, with onejar-maven-plugin”

  1. Erik Verheul says:

    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.

  2. Trishala says:

    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.

  3. Andrey says:

    So cool!!! Thanks!

  4. JG says:

    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

  5. Tonio says:

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

  6. Lauri says:

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

  7. Dawid says:

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

  8. Susan says:

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

  9. David says:

    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

  10. David says:

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

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

    tools.jar

  11. David says:

    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.

  12. David says:

    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.

  13. Sebastian says:

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

  14. Paddy Daly says:

    The shade maven plugin seems to handle spring 3 projects.
    http://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html

  15. Kev says:

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

  16. Stevo Slavic says:

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

  17. Jon says:

    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.

  18. Elisha says:

    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

  19. Elisha says:

    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 says:

      @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

  20. Ulrik Sandberg says:

    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:

    package com.example;
    
    @Component("transformer")
    public class MyTransformer {
    

    Spring config:

    <context:component-scan base-package="com.example" />
    

    Bootstrapping code:

    	public static void main(String[] args) {
    		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    		MyTransformer transformer = (MyTransformer) context.getBean("transformer");
    		transformer.run();
    	}
    

    I got the following exception:

    NoSuchBeanDefinitionException: No bean named 'transformer' is defined
    

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

    Spring config:

    <context:annotation-config />
    <bean id="transformer" class="com.example.MyTransformer" />
    

Leave a Reply