Share

October 10, 2008

Using Maven maven-assembly-plugin to build Env packages

Most web application require configurations based on the environment, Dev, Int, Stage and Prod do not always match each other...

I tend to keep these env configurations outside the war file. I use the maven-assembly-plugin to create tar files that gets deployed to the maven repository.

the following is how i use it..

<plugin>
 <artifactId>maven-assembly-plugin</artifactId>
 <configuration>
  <descriptors>
   <descriptor>src/assemble/dev.xml</descriptor>
   <descriptor>src/assemble/test.xml</descriptor>
   <descriptor>src/assemble/int.xml</descriptor>
   <descriptor>src/assemble/load.xml</descriptor>
   <descriptor>src/assemble/stage.xml</descriptor>
   <descriptor>src/assemble/prod.xml</descriptor>
  </descriptors>
 </configuration>
 <executions>
  <execution>
   <id>make-assembly</id>   
   <phase>package</phase>
   <goals>
    <goal>attached</goal>
   </goals>
  </execution>
 </executions>
</plugin>

after all that ... you need the xml files that desc how the files should be packaged

here is an example of one of them...

<assembly 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/xsd/assembly-1.0.0.xsd"
>
    <id>env-load</id>
    <formats>
        <format>tar</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/resources/env/load</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <source>target/classes/env-data.properties</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
    <includeBaseDirectory>false</includeBaseDirectory>
</assembly>

note that env-data.properties is used to keep the maven version

here is how i setup that up
<resources>
 <resource>
  <directory>src/main/resources/env</directory>
  </resource>
  <resource>
  <directory>src/main/resources/build</directory>
  <filtering>true</filtering>
  </resource>
</resources>

not that we have filtering = true

and this is the env-data.properties file... which at the end of the day the tar file will have the pom verison

env-data.version=${project.version}