Overview

Let us assume you have several environments like dev-01, dev-02, test-01, test-02 and finally prod. Furthermore we make the whole situation more or less simple and we assume having only a single module build which produces a single war file as result.

To use the Environments Maven Plugin you can simple create the following supplemental structure in your module apart of the war module configuration:

 src
  ├── main 
        ├── environments
             ├── dev-01
             │   └── first.properties
             ├── dev-02
             │   └── first.properties
             ├── test-01
             │   └── first.properties
             ├── test-02
             │   └── first.properties
             └── prod
                 └── first.properties

In result the Environments Maven Plugin will automatically create the appropriate war files containing the configuration file first.properties which might contain some information like the database connections url etc. for the appropriate environment.

The environment name (directory name) will automatically being used as classifier for the appropriate artifact. So we would get the following files after running Environments Maven Plugin via (assuming you have configured it correctly):

mvn clean package
  • artifactId-version-dev-01.war
  • artifactId-version-dev-02.war
  • artifactId-version-test-01.war
  • artifactId-version-test-02.war
  • artifactId-version-prod.war

If you need to add a new environment this can simply being achieved by adding a new directory under environments which might being called qa-01 plus the information you would like to configure and that’s it. Environments Maven Plugin will automatically identify the new environment by searching in the environment directory and producing an appropriate artifact out of it.

Those above packages contain the original war file content as well as the supplemental files/directories which have been given for the appropriate environments.

Example 2

In this example we would like to create configuration artifacts for each environment.

The configuration looks exactly the same in Example 1 except for the used goal. So you need to change the goal from environment to configuration in the plugin configuration. By using the:

mvn clean package

you will produce the following artifacts:

  • artifactId-version-dev-01.jar
  • artifactId-version-dev-02.jar
  • artifactId-version-test-01.jar
  • artifactId-version-test-02.jar
  • artifactId-version-prod.jar

As you might already realized that those files are not war files. The files are jar files which contain the configuration for each environment.

How To Configure

To configure Environments Maven Plugin you simply add the following to your pom file (we assume here a war file):

  <groupId>groupId</groupId>
  <artifactId>artifactId</artifactId>
  <version>0.1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>net.sf.environments-maven-plugin</groupId>
        <artifactId>environments-maven-plugin</artifactId>
        <version>0.3.1</version>
        <executions>
          <execution>
            <goals>
              <goal>environment</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Advantages

  • Much more convenient
  • less configuration.
  • Dynamically add new environments
  • No Profiles needed.