Creating runnable wars with Maven (and Jetty)

13 Feb

Hudson is a really great tool if you work with source code in a team with more than two persons. One thing in hudson always fascinated me: the fact, that Hudson is runnable just by typing

java -jar hudson.war

I do not know if it really has practical relevance, but I searched a way to create such a jar only with Maven and Jetty.

The problem is easy to understand: when starting Java with -jar, it expects a „normal“ file structure. So if the Main-Class is „de.jowisoftware.Main“, Java looks for de/jowisoftware/Main.class. The classloader does not understand the concept, that, in war files all class files are in WEB-INF/classes. If jetty is included in the war file, it is included as a jar file, so the classloader has to support nested archives. The default one doesn’t. Thankfully, there are solutions.

There already are a few blog posts which gave interesting hints. I decided to take an easy way: using overlays in the maven-war-plugin.

The idea of overlays is, that you can „mix in“ other zip or jar files, when the war file is created. This trick can be used to include additional resources like static images. It can also be used to include jetty and the startup logic, because overlays are extracted into the root of the war file. So first we need to create an overlay which starts Jetty. You find the following source all over the web:

The trick happens between the lines 25 and 27. Jetty is started with the own war file. The corresponding pom file looks like this:

Looks simple, does it? Next, use „mvn install“ to install the artifact into your local maven repository. Okay, now to the real project. Let’s assume we already have a simple webapp. It can be a simple Servlet, a wicket-application, a scala/lift application, anything. Also, let’s assume we already have a pom.xml for our project. We first add dependencies to it:

This leads to the effect that three jar files are added into the war file. That is exactly what we do not want. So we tell the maven-war-plugin to use the packages as overlay and to ignore the jar-files entirely when creating the list of dependencies. So again, in the pom.xml, we add/change the following lines:

Note the <packagingExcludes/> on line 17, which prevents maven from adding the jars into the war. The three overlay blocks let maven extract the jars and add the content into the root of the war file. So the war file looks like:

The mainClass-Entry specifies the default entry point. This is enough to do the following:

mvn package && java -jar target/my-war.war

Have fun

3 Replies to “Creating runnable wars with Maven (and Jetty)

  1. Maybe it would be a better way to use ant to move the class files of the own project into the root of the war file, because otherwise it would be a cyclic dependency and hudson or other build server would’nt build the project.


    1. Set the packaging to war
    2. Don’t use dependencies to your own project (in this case de.jowisoft)
    3. Use ant to copy the class files to the root of the build:


    move-main-class compile


    Thanks for the great guide here! 🙂

  2. Oh YES. It works!!!
    Thank you a lot. This was very helpful especially your sample of maven configuration! Thank you once more.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.