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 profiles to build and package artifacts configured for specific environments. See Introduction to Build Profiles for a more in-depth explanation of the profile concept.
Note:
This example assume the use of the Standard Directory Layout.
pom.xml src/ main/ java/ resources/ test/ java/
Under src/main/resources there are three files:
In the project descriptor, you need to configure the different profiles. Only the test profile is showed here.
<profiles> <profile> <id>test</id> <build> <plugins> <plugin> <artifactId>maven-antrun-plugin</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>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <artifactId>maven-jar-plugin</artifactId> <executions> <execution> <phase>package</phase> <goals> <goal>jar</goal> </goals> <configuration> <classifier>test</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> .. Other profiles go here .. </profiles>
Three things are configured in this snippet:
To activate this profile execute 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.