Building For Multiple Environments

As i described in my previous articles (Multiple Environments I and Multiple Environments II) there are situations where you need to create separate artifacts for different environments.

But the problem is that by using the suggested solutions is that it can be made better.

Thats the reason why I'm writing this blog article.

I have created a more or less simple Maven Plugin which handles exactly those situations. Let us assume you have the following environments:

  • dev-01
  • test-01
  • prod

And now you need to create configurations for those different environments which usually means you need to create a set of configuration files which are different for each of those environments.

So you simply create the following folder structure in your Maven project:

1src
2  โ”œโ”€โ”€ main 
3        โ”œโ”€โ”€ environments
4             โ”œโ”€โ”€ dev-01
5             โ”‚   โ””โ”€โ”€ first.properties
6             โ”œโ”€โ”€ test-01
7             โ”‚   โ””โ”€โ”€ first.properties
8             โ””โ”€โ”€ prod
9                 โ””โ”€โ”€ first.properties

So as an example the file first.properties contains some kind of configuration for your application.

By using the MultiEnv Maven Plugin you can now add the following to your pom.xml file:

 1  <groupId>groupId</groupId>
 2  <artifactId>artifactId</artifactId>
 3  <version>0.1-SNAPSHOT</version>
 4  <packaging>war</packaging>
 5  ...
 6  <build>
 7    <plugins>
 8      <plugin>
 9        <groupId>com.soebes.maven.plugins</groupId>
10        <artifactId>multienv-maven-plugin</artifactId>
11        <version>0.1.0</version>
12        <executions>
13          <execution>
14            <goals>
15              <goal>environment</goal>
16            </goals>
17          </execution>
18        </executions>
19      </plugin>
20    </plugins>
21  </build>

As a result you can simply call Maven:

1mvn clean package

which will result in three supplemental files:

  • artifactId-0.1-SNAPSHOT-dev-01.war
  • artifactId-0.1-SNAPSHOT-test-01.war
  • artifactId-0.1-SNAPSHOT-prodc-01.war

inclusive the base artifactId-0.1-SNAPSHOT.war file (Here I have assumed you are creating a war file).

Those files like *-dev-01.war contain the configuration of the appropriate folder which means you can deploy them into the appropriate environment.

The other solution is to create separate configuration packages which contain only the configuration for the separate environments. This can be done with the MultiEnv-Maven-Plugin as well. It provides a different goal for this purpose:

 1  <groupId>groupId</groupId>
 2  <artifactId>artifactId</artifactId>
 3  <version>0.1-SNAPSHOT</version>
 4  <packaging>war</packaging>
 5  ...
 6  <build>
 7    <plugins>
 8      <plugin>
 9        <groupId>com.soebes.maven.plugins</groupId>
10        <artifactId>multienv-maven-plugin</artifactId>
11        <version>0.1.0</version>
12        <executions>
13          <execution>
14            <goals>
15              <goal>configuration</goal>
16            </goals>
17          </execution>
18        </executions>
19      </plugin>
20    </plugins>
21  </build>

So you can call Maven:

1mvn clean package

which will result in three supplemental files:

  • artifactId-0.1-SNAPSHOT-dev-01.jar
  • artifactId-0.1-SNAPSHOT-test-01.jar
  • artifactId-0.1-SNAPSHOT-prodc-01.jar

Those artifacts only contain the configuration files of the appropriate environments and can be deployed to the appropriate environments as a separate package.

If you have ideas/wished/bugs etc. please use the issue tracking system of the plugin on Github.

Any feedback is appreciated.