Maven2 Assembly plugin

A sample project with pom.xml and assembly descriptor to help get you started

Published: Tuesday, 14 June 2011

Creating a distribution

This plugin is used for creating distribution packages. e.g. if you want to publish a single tar.gz that contains documentation and other setup scripts, you can use this plugin to help create it as part of your maven build.

I’ve used this to create a single tar.gz that can be deployed to a fresh linux server that will install and run our application.

pom.xml

Below is an example maven project pom.xml that uses the assembly plugin. Note that there is a dependency on an existing war artifact so it can be included in the distribution.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.magicmonster</groupId>
  <artifactId>distribution</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>example :: distribution</name>
  <description>distribution</description>
  <dependencies>
    <dependency>
      <groupId>com.magicmonster</groupId>
      <artifactId>example</artifactId>
      <version>1.0-SNAPSHOT</version>
      <type>war</type>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.2.1</version>
        <configuration>
          <descriptors>
            <descriptor>src/main/descriptors/example.xml</descriptor>
          </descriptors>
        </configuration>
      </plugin>
    </plugins>
  </build>

The file src/main/descriptors/example.xml contains further information on how to build the single tar.gz.

Contents of descriptor

Below is the contents of the descriptor, src/main/descriptors/example.xml

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
  <id>example</id>
  <formats>
    <format>tar.gz</format>
  </formats>
  <!-- copy src/release directory -->
  <fileSets>

    <fileSet>
      <directory>src/release/</directory>
      <outputDirectory>/</outputDirectory>
      <fileMode>0755</fileMode>
    </fileSet>
  </fileSets>
  <dependencySets>

    <!-- Copy war file -->
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <unpack>false</unpack>
      <includes>
        <include>com.magicmonster:example:war</include>
      </includes>
    </dependencySet>
  </dependencySets>
</assembly>

It contains instructions to create a tar.gz archive. You can pick other archive formats too, such as zip.

The 0755 is used for unix permission modes.

The directory src/release is the top level directory for the distribution. Files are copied across from there. You can add deploy scripts and documentation here.

To include existing wars from your maven repo, you can use the dependencySet to get a copy of them.

I’m not sure how to rename the war file as part of the mvn build, and have worked around this using the deploy script.

mvn assembly:single

Use the command mvn assembly:single to trigger this plugin.

Documentation

Maven Assembly Plugin