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:
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<configuration>
<mainClass>your.package.YourMainClass</mainClass>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
You also need to add this to the pom:
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
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:
mvn install
That will build both your normal jar, as well as another jar. You will get these:
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:
java -jar target/myApp.one-jar.jar
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
Hugo JosefsonConsultant at Jayway








7 comments ↓
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:
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:
I then switched to this config, still using annotation-based dependency autowire, but requiring an explicit bean definition:
Spring config:
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.
@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
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
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.
Just to confirm on what Ulrik wrote, component scanning doesn’t work with onejar. Not sure yet whom to blame.
Another confirmation that Spring component scanning doesn’t work with onejar. Bummer. Confirmation of Ulrik and Stevo’s findings.
Leave a Comment