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:

Plugin Version
assembly 2.4.1

Sample Generated Output

  1. attach-source-javadoc
  2. |-- pom.xml
  3. |-- src\
  4. `-- target
  5. `-- apache-maven-cookbook-1.0-SNAPSHOT-bin
  6. `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.bz2
  7. `-- apache-maven-cookbook-1.0-SNAPSHOT-bin.tar.gz
  8. `-- 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.

  1. <formats>
  2. <format>tar.gz</format>
  3. <format>tar.bz2</format>
  4. <format>zip</format>
  5. </formats>

And the wanted files sets, i.e.

  1. <fileSets>
  2. <fileSet>
  3. <includes>
  4. <include>README*</include>
  5. </includes>
  6. </fileSet>
  7. <fileSet>
  8. <directory>src/bin</directory>
  9. <outputDirectory>bin</outputDirectory>
  10. <includes>
  11. <include>*.bat</include>
  12. </includes>
  13. <lineEnding>dos</lineEnding>
  14. </fileSet>
  15. <fileSet>
  16. <directory>src/bin</directory>
  17. <outputDirectory>bin</outputDirectory>
  18. <includes>
  19. <include>hello</include>
  20. </includes>
  21. <lineEnding>unix</lineEnding>
  22. <fileMode>0755</fileMode>
  23. </fileSet>
  24. <fileSet>
  25. <directory>target</directory>
  26. <outputDirectory>lib</outputDirectory>
  27. <includes>
  28. <include>generate-assembly-*.jar</include>
  29. </includes>
  30. </fileSet>
  31. </fileSets>

Configuring Maven Assembly Plugin

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

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-assembly-plugin</artifactId>
  4. <configuration>
  5. <descriptor>src/assembly/bin.xml</descriptor>
  6. <finalName>apache-maven-cookbook-${pom.version}</finalName>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <phase>package</phase>
  11. <goals>
  12. <goal>single</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>

Running Maven

Just call Maven to generate the packages:

  1. mvn package