Maven custom packaging with the assembly plugin

It's not uncommon to need a custom packaging or assembly for a given project for some reason or another, this can be accomplished in a certain number of ways

Recently I needed an application to be packaged as the following structure :

|- runnable-jar.jar
|- lib
    |-- axis.jar
    |-- commons.logging.jar 
|- certificates
   |-- cacerts.jks
|- config
|- logs

The schema is pretty self-explanatory I needed a runnable JAR at the root of my dist folder with a few other folders such as config, logs, etc.

One way to accomplish this with maven is to use maven-assembly-plugin

The Assembly Plugin for Maven is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive.

Your project can build distribution assemblies easily, using one of the convenient, prefabricated assembly descriptors.

These descriptors handle many common operations, such as packaging a project's artifact along with generated documentation into a single ZIP archive. Alternatively, your project can provide its own descriptor and assume a much higher level of control over how dependencies, modules, file-sets, and individual files are packaged in the assembly.

Currently the plugin can create distributions in the following formats:

  • zip
  • tar
  • tar.gz
  • tar.bz2
  • jar
  • dir
  • war
  • format that the ArchiveManager has been configured

For this tutorial we will be using the dir format in order to output the project structure described above

Let's start with the easy part first, the maven assembly plugin configuration where we will tell the plugin where to find our assembly file and where to output the result of the assembly operation




Now since in our case we want all the files in the resource directory to be handled by the assembly plugin we will configure the maven resources plugin to ignore everything inside the resources folder (please note that this is an optional step and it might not apply to your project)




Once the maven part is done we can get started with the core code

If you remember we will end up with a runnable JAR file so go ahead and create a Main class like so :

package com.ufasoli.maven.custom.assembly;

 * User: Ulises Fasoli
 * Date: 23.06.2014
 * Time: 18:03
 * Project : maven-custom-assembly
public class Main {

    public static void main(String[] args) {
        System.out.println("HELLO WORLD !");

Since this is going to be a runnable JAR file we will need to configure the maven JAR plugin to generate the manifest file with our main class


Now we get to the core of the subject, the assembly file :







Now this file is pretty self-explanatory but below are the highlights :

  • format --> the output format for our assembly (directory in our case)
  • dependencySets--> which artifacts to include in the final structure and where to put them
  • fileSets--> which files to include in the final structure and where to put them

Now you can build your assembly by running the following maven goal :

    mvn clean package assembly:single

Once the goal completed under the /target folder you should find your custom project structure

As usual you can find the code source for this tutorial over at my github account here

Popular posts from this blog

Multi-hop ssh tunnel - howto : Creating a SSH tunnel with port forwarding between multiple hosts

Spring boot and spring data jpa tutorial - A sample application using spring 4.0 spring boot and JPA (part 2/2)

Maven build number, versioning your projects builds automatically