Artikel Terbaru

Tuesday, 31 January 2012

maven assembly plugin example with tutorial in maven2


Maven assembly plugin in maven2:-


Maven is project build management tool , it automate the build phase of any java

projects.whenever the project is build, it crease single modules with the format of jar or war.
so it builds and zip your project including configuration files and other xml files into different customized output module types lik zip,jar,wat 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. More over, it generates single war file.

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

if you want to generate a zip file (CloudHadoop.zip)of your project source code with it's 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 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 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 sub folders)

descriptor specifies the cusotmized 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>

format specifies the result for format of you 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:-

assembele plugin can be run in two ways.

one way with independent way of execution.

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

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 single execution

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

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