Cookbook: How To Generate Assembly?

Summary

This recipe describes how to generate assembly like zip, tar.gz or tar.bz2.

Prerequisite Plugins

Here is the list of the plugins used:

PluginVersion
assembly2.2-beta-1

Sample Generated Output

attach-source-javadoc
|-- pom.xml
|-- src\
`-- target
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.bz2
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.gz
    `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.zip

Recipe

Configuring Assembly Descriptor

To generate an assembly, we need to configure an assembly descriptor called bin.xml in the src/assembly directory. Firstly, we specify the wanted formats, i.e.

<formats>
  <format>tar.gz</format>
  <format>tar.bz2</format>
  <format>zip</format>
</formats>

And the wanted files sets, i.e.

<fileSets>
  <fileSet>
    <includes>
      <include>README*</include>
    </includes>
  </fileSet>
  <fileSet>
    <directory>src/bin</directory>
    <outputDirectory>bin</outputDirectory>
    <includes>
      <include>*.bat</include>
    </includes>
    <lineEnding>dos</lineEnding>
  </fileSet>
  <fileSet>
    <directory>src/bin</directory>
    <outputDirectory>bin</outputDirectory>
    <includes>
      <include>hello</include>
    </includes>
    <lineEnding>unix</lineEnding>
    <fileMode>0755</fileMode>
  </fileSet>
  <fileSet>
    <directory>target</directory>
    <outputDirectory>lib</outputDirectory>
    <includes>
      <include>generate-assembly-*.jar</include>
    </includes>
  </fileSet>
</fileSets>

Configuring Maven Assembly Plugin

We execute the assembly:single goal from the Assembly plugin during the package phase.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <descriptor>src/assembly/bin.xml</descriptor>
    <finalName>apache-maven-cookbook-${pom.version}</finalName>
  </configuration>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

Running Maven

Just call Maven to generate the packages:

mvn package