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.)
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!
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.
It may sound weird, but it’s quite elegant! Just put this in your
<plugins> tag to make it work:
org.dstovall onejar-maven-plugin 1.3.0 your.package.YourMainClass one-jar
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!
Then do this to build everything:
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.
Project homepage: http://onejar-maven-plugin.googlecode.com