Develop an environment-aware Maven build process

Extend Maven builds to port Java apps to multiple environments

1 2 3 4 5 Page 3
Page 3 of 5

Figure 4 shows Web Service Client's Maven project directory structure as viewed in Eclipse Indigo.

Figure 4. Web Service Client's directory structure


As the pom.xml files for the Database and Web Service Client Maven projects show, the property files are automatically included in the JARs when they are built, as is usual for Maven projects. There are two ramifications to this automatic inclusion:

  1. The JARs themselves become environmentally specific.
  2. It is difficult to quickly see and modify the property values once the application has been deployed.

Neither of these outcomes is desirable. In fact, a process that did not include the property files bundled into the JARs would be more practical. We'll deal with that a bit later in the article.

App Config

Now that we have the pom.xmls and the project directory structure for each project laid out we can begin to extend our Maven build to be environment-aware. Our first task is to create a new Maven project that contains all of the property files in a templatized format. The App Config project's pom.xml is shown in Listing 4.

Listing 4. App-Config pom.xml

<project xmlns="" xmlns:xsi=" 2001/XMLSchema-instance"

    <description>Contains config files</description>

Figure 5 shows App-Config's Maven project directory structure as viewed in Eclipse Indigo.

Figure 5. App-Config Maven project directory structure

Notice that the directory structure for this project contains not only the templatized property files for both the Database project and the Web Service Client project, but also a property file called Listings 5 through 7 display the contents of all three of these property files.

Listing 5. Property file:${}

Listing 6. Property file:


Listing 7. Property file:

###### WEB SERVICE SERVER ###############

###### SQL SERVER 2008 #################

With the App-Config Maven project set up, we can begin on the next phase of our solution, building Parent-Pom.


Parent-POM is a new parent Maven project that utilizes App-Config. It will provide each Maven project that inherits the Parent-POM with its own set of property files, but configured for a particular environment: development, testing, or production. Like other Maven projects, Parent-POM contains a pom.xml; however, unlike the other Maven projects, the packaging type is not JAR but POM. Below is Parent-POM’s pom.xml.

Listing 8. Parent-POM’s pom.xml

<project xmlns="" xmlns:xsi=""

    <description>Contains the parent pom configurations</description>


                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                    <!-- Content not displayed in this article for brevity purposes. -->

The pom.xml in Listing 8 utilizes the maven-dependency-plugin to pull from the Maven repository the App-Config project’s JAR, extract the contents of the JAR, and place them in ${}/alternate-resources.

Note that the path itself utilizes an implicit Maven property key,, which has a value of whatever project references the Parent-POM project. At the very bottom of the pom.xml, the <filter> element automatically loads all of the property keys and values in the property file, and makes those values available during the property-filtering process of the maven-resources-plugin.

1 2 3 4 5 Page 3
Page 3 of 5