Maven replace tokens inside files before application build using profiles

Chris VezalisDEVOPS, Maven, Oracle ADF

When building projects on multiple environments (for example a developer test server, a system test server, a user acceptance test server, a training server and finally a production server), application need to be passed environment variables. One way to solve this is to use tokens on files that Maven will replace will real values found on separate property files for each environment. Then we can use Maven profiles for load the appropriate properties for each environment.

An example of how to create user profiles in pom file is the following:

<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>dev</build.profile.id>
</properties>
</profile>
<profile>
<id>sat</id>
<properties>
<build.profile.id>sat</build.profile.id>
</properties>
</profile>
</profiles>

Then you can execute the pom file using a parameter (-P) to send the appropriate profile for the environment:

mvn -P sat clean install

Then you can load the properties for your environment using the load properties maven plugin:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/profiles/${build.profile.id}/config/build.properties</file>
</files>
</configuration>
</execution>
</executions></plugin>

This will load the property file using the build.profile.id variable that we declare on our profiles. The result is that the system will load the correct properties for our environment.
After you have declare your profiles for each environment and load the properties you can use tokens on property files or other files that maven can replace them with actual values. The plugin that replace that tokens is the maven-resources-plugin. The following is an example for using the maven-resources-plugin:

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/src/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}/profiles/dev/resources</directory>
<includes>
<include>MANIFEST.MF</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>app-properties</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<overwrite>true</overwrite>
<outputDirectory>${basedir}/Model/src</outputDirectory>
<resources>
<resource>
<directory>${basedir}/profiles/dev/resources</directory>
<includes>
<include>AppParameters.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

The trick here is the true. When this is true Maven will copy the files to the appropriate locations and it will also replace the tokens inside that files. The tokens inside that files can be in the following format ${property.name}.
In this example we first replace a token in the Manifest file to create for example a version for ADF Java application. After we replace several properties in an AppParametes.properties file.
You can use this method to replace unlimited tokens in several files before building the application for a specific environment.

Chris Vezalis