Better build configurations with Maven
Submitted by thijs on 13 June, 2008 - 15:02For 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
- WEB-INF
- acc
- WEB-INF
- applicationContext-local.xml
- web.xml
- WEB-INF
- tst
- WEB-INF
- applicationContext-local.xml
- web.xml
- WEB-INF
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.

Post new comment