Google

Jul 12, 2014

Maven assembly plugin example

Maven assembly plugin is primarily intended to allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distribution archive like zip, tar, tar.gz, war, etc. Another example where this is handy is to create deployable files for different environments like myapp-1.10-SNAPSHOT-dev.zip, myapp-1.10-SNAPSHOT-test.zip, and myapp-1.10-SNAPSHOT-prod.zip where each containing environment related properties files packaged.

3 steps to create an assembly

  1. choose or write the assembly descriptor files to use like dev-assembly.xml, prod-assembly.xml, etc
  2. configure the Assembly Plugin in your project's pom.xml, and include the above descriptor files
  3. run "mvn assembly:single" on your project.


For example:

The pom.xml file

<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.app/groupId>
 <artifactId>myapp</artifactId>
 <packaging>war</packaging>
 <name>myapp</name>
 <version>1.10-SNAPSHOT</version> 
 
 ...
 
 <plugins>
     ....
     <plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <version>2.2.1</version>
   <executions>
    <execution>
     <id>assemble</id>
     <phase>package</phase>
     <configuration>
      <descriptors>
       <descriptor>assembly/dev-assembly.xml</descriptor>
       <descriptor>assembly/test-assembly.xml</descriptor>
       <descriptor>assembly/prod-assembly.xml</descriptor>
      </descriptors>
     </configuration>
     <goals>
      <goal>single</goal>
     </goals>
    </execution>
   </executions>
  </plugin>
 
 
 <plugins>
 
 ....
</project>


Now the dev-assembly.xml file

<assembly
 xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
 <id>dev</id>
 <includeBaseDirectory>false</includeBaseDirectory>
 <formats>
  <format>zip</format>
 </formats>
 <fileSets>
  <fileSet>
   <directory>config/dev</directory>
   <outputDirectory>/app-properties</outputDirectory>
  </fileSet>
  <fileSet>
   <directory>jboss-config/dev/deploy</directory>
   <outputDirectory>/ds</outputDirectory>
  </fileSet>
 </fileSets>
 <files>
  <file>
   <source>jboss-config/common/myapp-container.properties</source>
   <outputDirectory>/myapp-container-properties</outputDirectory>
   <destName>my-app-dev.properties</destName>
  </file>
 </files>
</assembly>


The prod environment prod-assembly.xml will have something like

<assembly
 xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 
 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
 <id>prod</id>
  <includeBaseDirectory>false</includeBaseDirectory>
 <formats>
  <format>zip</format>
 </formats>
 <fileSets>
  <fileSet>
   <directory>config/prod</directory>
   <outputDirectory>/app-properties</outputDirectory>
  </fileSet>
  <fileSet>
   <directory>jboss-config/prod/deploy</directory>
   <outputDirectory>/ds</outputDirectory>
  </fileSet>
 </fileSets>
 <files>
     <file>
      <source>jboss-config/common/myapp-container.properties</source>
      <outputDirectory>/container-properties</outputDirectory>
      <destName>my-app-prod.properties</destName>
    </file>
   </files>
</assembly>


The main goal in the assembly plugin is the single goal. It is used to create all assemblies.

Now to generate different packages like myapp-1.10-SNAPSHOT-dev.zip, myapp-1.10-SNAPSHOT-test.zip, and myapp-1.10-SNAPSHOT-prod.zip

mvn package


Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home