Better build configurations with Maven

For deploy of a project you often need a different configuration for production, acceptation, test and etc. Maven is the tool to use.

Use the following in your maven2 pom.xml file:

Profiles:

  <!-- =====================================================
    Profiles
====================================================== -->
  <profiles>
    <profile>
      <id>deployment-default</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
        <deployment-env>development</deployment-env>
        <build.directory>target</build.directory>
        <finalName>ROOT</finalName>
      </properties>
    </profile>
    <profile><!-- run with: -Denv=.. (see etc/* for environments) -->
      <id>deployment-environment</id>
      <activation>
        <property>
          <name>env</name>
        </property>
      </activation>
      <properties>
        <deployment-env>${env}</deployment-env>
        <finalName>ROOT</finalName>
        <build.directory>target/build/${env}</build.directory>
      </properties>
    </profile>
  </profiles>

Build:

  <!-- =====================================================
    Build
====================================================== -->     
  <build>
    <finalName>${finalName}</finalName>
    <directory>${build.directory}</directory>    
    <plugins>
      <!-- =====================================================
        War plugin
        copy extra files for the specified environment (see profiles)
====================================================== -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.0</version>
        <configuration>
          <webResources>
            <resource>
              <!-- this is relative to the pom.xml directory -->
              <directory>etc/${deployment-env}</directory>
            </resource>
          </webResources>
        </configuration>
      </plugin>      
    </plugins>
  </build>

Create a directory in etc/ for every environment you want to build. You must create a etc/development directory for the default.

example:

  • etc/
    • development
    • prd
      • WEB-INF
        • applicationContext-local.xml
        • web.xml
    • acc
      • WEB-INF
        • applicationContext-local.xml
        • web.xml
    • tst
      • WEB-INF
        • applicationContext-local.xml
        • web.xml

To build the test environment run: mvn package -Denv=tst

target/tst/ROOT.war will be created

The choice of env is slightly unfortunate since it has special meaning to maven. It can be used to lookup environment variables like ${env.PATH}. dest or targetEnv or something alike would be better.

Trackback URL for this post:

http://www.func.nl/trackback/50

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".