Tuesday, January 31, 2012

maven assembly plugin example with tutorial in maven

Maven assembly plugin in maven2:-

Maven assembly plugin tutorials with examples

Maven is a project build management tool, it automates the build phase of any java projects.whenever the project is built, it creates single modules with the format of jar or war.
so it builds zip your project including configuration files and other XML files into different customized output module types like zipping, jar, war, and other tar.gz formats.

As you know maven-war-plugin generates only war files, but in the projects, you need to archive in different formats like zipped formats, in that case, we will use this plugin.

usually, any project builds either war or jar file. so if the project(artifact name is CloudHadoop), the result of your install command is CloudHadoop-1.0.0-SNAPSHOT.war. Moreover, it generates a single war file.

But using this plugin, we can multiple zip files like the jar, zip with one execution

if you want to generate a zip file (CloudHadoop.zip)of your project source code with its dependencies as well as configuration files, There are three task involved to use the maven-assembly-plugin in our project.

1.Configure maven-assembly-plugin plugin
2.Create custom assembly file
3.Run assembly maven goal command

Configure the maven-assembly-plugin plugin:-

Maven is plugin based architecture so you need to configure plugin if we want to support any extra functionality.

Add the following plugin code in your pom.xml

<project>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <groupId>org.apache.maven.plugins</groupId>
        <version>2.2.0</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>project</descriptorRef>
          </descriptorRefs>
    <descriptors>
            <descriptor>src/assembly/zipCode.xml</descriptor>
          </descriptors>
   <finalName>${pom.artifactId}</finalName>
       </configuration>
  <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
</project>

maven-assembly-plugin is plugin is defined with plugin element with groupId and version
descriptorRef represents the type of folder structure in the zipped bundled file.

the possible elements for descriptorRef are jar-with-dependencies(jar format like MANIFEST), project(zipped in project directory structure), src (only the src folder with subfolders)

descriptor specifies the customized assembly XML which contains the code

execution phase represents the goal where this assembly code is executed

2.create custom assembly descriptor file:-


we can define our own custom assembly file to create our module. and the file name is zipCode.xml. you can define multiple assembly files by configuring this files in plugin declaration.
<assembly>
  <id>src</id>
  <formats>
    <format>zip</format>
  </formats>
  <includeBaseDirectory>true</includeBaseDirectory>
  <fileSets>
<fileSet>
<directory>src/main</directory>
<outputDirectory>zipDirectory</outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
</assembly>

the format specifies the result for the format of your zipped file.
FileSets defined the files in the source directory of your project. and target directory is zipDirectory after your package goal is run.

Building your project with assembler:-

assemble plugin can be run in two ways.

one way with an independent way of execution.

  mvn assembly:assembly 
.once the above goal is executed, it creates a zipped file which contains files in the form of same project directory structure.

and other ways is attach the plugin execution with any predefined goals.

maven package

How to create multiple jar files with maven:-

listing down highlevel steps to generate multiple jar files with in the single execution

  • maven-assembler-plugin configuration in pom.xml
  • define multiple exections element in maven-assembler-plugin plugin configuration and configure multiple fule custom assembler xml files
  •  For each jar, define own custom assembler file with format type and source and target directories information and also include include and exclude elements
  •  define the goal execution as single or attached with any predefined goals
  • Finally, build your project using maven command in the command line.


Hope you understand basic idea on maven2 assembler plugin with a basic example.

Related article


EmoticonEmoticon