|
[Maven]
Building For Different Environments with Maven 2
[
trygvis
]
Building the same artifact for different environments has always been an
annoyance. You have multiple environments, for instance test and production
servers or, maybe a set of servers that run the same application with different
configurations. In this guide I'll explain how you can use Note:
This example assume the use of the Standard Directory Layout[2]. A fully-working example project can be found at [3].
pom.xml
src/
main/
java/
resources/
test/
java/
Under
In the project descriptor, you need to configure the different profiles. Only the test profile is showed here, see the accompanying source code[3] for the full pom.xml.
<profiles>
<profile>
<id>test</id>
<build>
<plugins>
<plugin>
<artifactId>mavenltantrunltplugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete file="${project.build.outputDirectory}/environment.properties"/>
<copy file="src/main/resources/environment.test.properties" tofile="${project.build.outputDirectory}/environment.properties"/>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>mavenltsurefireltplugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>mavenltjarltplugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>test</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
.. Other profiles goes here ..
</profiles>
Three things are configured in this snippet:
mvn -Ptest install
and maven will
execute the steps in the profile in addition to the normal steps. From this
build you will get two artifacts, "foo-1.0.jar" and "foo-1.0-test.jar". These
two jars will identical.
Caveats
Resources
Doesn't work. If you use mvn -Pprod install it uses the default properties. I think this is caused by the phase: prod If you change to test it works OK. Unfortunately I don't know enough about Maven / Ant etc to guess why. Other than that, thanks for doing this. Once I get it working it will be just what I needed. Regds, --Max, October 10, 2006 03:20 PM
It looks like the maven-antrun-plugin phase for the profile "prod" is prod. Where I think it should be test. That might be why it wasn't working for you. -Scot --Scot Hale, March 24, 2007 12:11 AM
Thanks --Javier, August 21, 2008 07:02 PM
In my case, the same generated jar needs to be pushed across all the environments till production (cant have separate profiles). Any suggestions on how to put property files on maven classpath so that its independant of environment? --Maven Beginner, April 30, 2009 07:40 PM
Post a comment
|