Maven: Configuration For Multipe Environments - II

Based on a comment on my previous article i would like to show how to add dependencies to the generated artifacts depending on the environment.

The most important problem is: How to get the different artifacts? If you have artifacts which are stored in a Maven Repository like Maven Central it is easy to get them. Just use the Maven Dependency Plugin to get the artifacts. This means you have to add the following to your pom file:

 1<plugin>
 2  <groupId>org.apache.maven.plugins</groupId>
 3  <artifactId>maven-dependency-plugin</artifactId>
 4  <version>2.3</version>
 5  <executions>
 6    <execution>
 7      <id>test</id>
 8      <phase>prepare-package</phase>
 9      <goals>
10        <goal>copy</goal>
11      </goals>
12      <configuration>
13        <artifactItems>
14          <artifactItem>
15            <groupId>junit</groupId>
16            <artifactId>junit</artifactId>
17            <version>4.1</version>
18            <type>jar</type>
19          </artifactItem>
20        </artifactItems>
21        <overWrite>false</overWrite>
22        <outputDirectory>${project.build.directory}/environment/test</outputDirectory>
23      </configuration>
24    </execution>

Apart from that you need a separate execution block for every environment (test, qa and production in our case). Furthermore if you need more than one dependency you can simply achieve this by adding them in the artifactItems block with their appropriate groupId, artifactId etc. So far so good. But what happens if you need artifacts which are not located within an Maven like repository? Ok not problem at all. You can use the wagon-maven-plugin to download the appropriate artifacts. To do such things you simply enhance your pom with the appropriate configuration for the wagon-maven-plugin:

 1<plugin>
 2  <groupId>org.codehaus.mojo</groupId>
 3  <artifactId>wagon-maven-plugin</artifactId>
 4  <version>1.0-beta-4</version>
 5  <executions>
 6    <execution>
 7      <id>download-test-data</id>
 8      <phase>prepare-package</phase>
 9      <goals>
10        <goal>download-single</goal>
11      </goals>
12      <configuration>
13        <url>http://archive.apache.org/dist/abdera/1.1.2/</url>
14        <fromFile>apache-abdera-1.1.2-src.tar.gz</fromFile>
15        <toDir>${project.build.directory}/environment/qa</toDir>
16      </configuration>
17    </execution>
18  </executions>
19</plugin>

In this case i have added a .tar.gz archive downloaded from Apache archive and added that to the qa environment. The full code of a full fledged Maven build for that and the previous article can be found here.