Just Enough Developed Infrastructure

Maven - Executable Fat Jars With Non Mavenized Jars

Maven, the final frontier... This is one of these beasts that can really give you some fun time: some would say, it's merely a build tool. But sometimes the complexity and syntax and dependencies makes me think this is like a profession apart.

I'm writing this blogpost for my own documentation purposes but it might prove you useful.

The task I was facing was to:

  1. include jars that didn't have maven version i.e. could not simply be added by adding a maven dependency
  2. build a fat jar that includes all the jars it needs instead of only itself
  3. make the jar file executable

Non-maven jars in your project

I got most information from Can I add jars to maven 2 build classpath without installing them?

The trick seems to be to create a directory inside your java project that can act as a maven repo that you can refer to based upon the project ${basedir} variable.

<% codify(:shell) do %> $ cd my-project $ mkdir my-repo $ mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file -Dfile=/Users/patrick/cooljar.jar -DgroupId=be.jedi -DartifactId=cooljar -Dversion=1.5.1 -Dpackaging=jar -DlocalRepositoryPath=pwd/my-repo <% end %>

Now you can add this part to your maven pom file: <% codify(:xml) do %>

be.jedi cooljar 1.5.1 compile my-local-repo file://${basedir}/my-repo <% end %>

Also have a look at A maven friendly pattern for storing dependencies in version control for an alternative.

Building a fat jar & make it executable

The document I used to figure out this combination where:

I struggled with the combination of the two and finally found the answer in this Stack Overflow post on the exact problem I had

It seems that depending on the versions of the archiver and Assembly plugin you have to move things around in the maven config file. And if you're on jdk 1.6 you need extra options. The following worked for me:

<% codify(:xml) do %> maven-assembly-plugin 2.2-beta-4 jar-with-dependencies true be.jedi.jvncsender.VncSenderMain make-assembly package single org.apache.maven.plugins maven-jar-plugin 2.2 org.apache.maven.plugins maven-compiler-plugin 1.6 1.6
<% end %>