SoftwareEntwicklung Beratung Schulung

A coders, hackers heaven.....Hm...I do not think so...

Building for Multiple Environments

| Comments

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:

1
2
3
4
5
6
7
8
9
src
  ├── main 
        ├── environments
             ├── dev-01
             │   └── first.properties
             ├── test-01
             │   └── first.properties
             └── prod
                 └── 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>com.soebes.maven.plugins</groupId>
        <artifactId>multienv-maven-plugin</artifactId>
        <version>0.1.0</version>
        <executions>
          <execution>
            <goals>
              <goal>environment</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

As a result you can simply call Maven:

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

So you can call Maven:

1
mvn 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.

Comments