Creating web applications with maven

To create a web application with maven the powerful Maven war plugin is the best choice. Especially when combining dependencies and other resources into the web application archive. This post explains two techniques: overlays and external resources.

This simple web app which I will use as an example contains a multi module build with three projects:

  • static-content. A project which contains static content like images
  • remote-resources. Content which has been published using the remote resource plugin
  • web-app. The web application which aggregates the content of the projects above

The static-content project deploys it’s content as a zip using the maven assembly plugin. The configuration in the pom looks like this:

This tells maven to publish the content described in static-content.xml in a zip. This so called assembly descriptor contains information what to include in the zip:

In this case the content of the folder content/images should be included.

Adding this content to the war using overlays is quiet simple. Simply add the project as a runtime dependency to the web app project and configure an overlay to use it:

The resulting web app structure looks like this:

.
├── images
│   └── smiley.jpg
├── META-INF
└── WEB-INF
├── classes
└── web.xml

Another way that obviates the need for zips is to include the resources using the Maven remote resource plugin. The remote-resource project’s pom contains the usage which is really straight forward:

The inclusion in the war is however is a little bit more complicated. It includes adding the resource and then declaring a web resource in the war’s configuration:

The above excerpt of the project’s pom is more or less self-exploratory. The resulting war structure looks like this:

.
├── disclaimer.html
├── META-INF
└── WEB-INF
├── classes
└── web.xml

So which way of including resources in a war is the better one ?

The answer is: It depends. The natural way would be to use overlays. The remote resource plugin approach on the other hand spares you from having to write an assembly descriptor.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">