Web application environment configuration using maven profiles

Ok, this blog post will be short. I will just share some small maven code snippets to configure your build and development IDE environment.

For some projects there is a need to use specific environment properties, which may differ from deployment environment. Let's say in your IDE you use "Development" mode, however it is obvious that test, QA and production environments will use "Deployment" mode.

Usually such property is stored in web.xml file, for example:

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>${jsf.projectStage}</param-value>
</context-param>

Issue

  1. We do not want to commit "Development" mode into source control.
  2. We do not want to use "Deployment" mode in our local IDE.
  3. We do not want to have uncommitted changes if using "Development" mode locally.
  4. We DO want properly running local environment :) Sure!

By default property ${jsf.projectStage} is not properly understood by IDE (at least Eclipse WTP) and is not transformed into real value. Well, maybe it should be a nice feature... However, if you are using Apache Maven then this can be easily fixed.

Talks and documentation

  1. Introduction to Build Profiles
  2. Eclipse + maven : profile ignored
  3. And the solution is on stackoverflow described as well, however i could not find the link to it anymore when writing this blog :) I hope you will share this link with me :)

Preconditions

You need this environment setup in order to successfully complete the provided guide:

Solution

So far everything is about your pom.xml file configuration. You need to tell maven what resources should be filtered. Also tell maven which property file to use for resource filtering. And resolve this property file name dynamically using filter.name maven property.

<build>
    <!-- Add filter file name -->
    <filters>
        <filter>${basedir}/src/main/filters/${filter.name}.properties</filter>
    </filters>

    <plugins>
        <!--  Define filtered resources -->
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <webResources>
                    <resource>
                        <directory>${basedir}/src/main/webapp</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>WEB-INF/web.xml</include>
                            <include>WEB-INF/jboss-web.xml</include>
                        </includes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
    </plugins>
</build>

Then define different profiles you will need. And tell each profile which property file should be applied (<filter.name>production</filter.name>).

<profiles>
    <profile>
        <id>default</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>production</id>
        <properties>
            <filter.name>production</filter.name>
        </properties>
    </profile>
</profiles>

But we would like to have a default configuration for local environment. Let's define it in pom.xml as well.

<properties>
    <!-- Default name of file for filtering -->
    <filter.name>localhost</filter.name>
</properties>

Usage

Now, eclipse IDE should automatically resolve and use default properties when you import maven project. For continuous delivery and console based builds you might be using such command:

mvn clean package -Pproduction

-P may be avoided if you want to use default properties, e.g. for development build.

Properties file location

Yes, you may define your own property files location based on your project structure:

<filters>
 <filter>${basedir}/src/main/filters/${filter.name}.properties</filter>
</filters>

Resources

Hopefully you did enjoy reading!

vytas

Senior software engineer, software architect for java enterprise based solutions