Dynamic Configuration Switching in Maven Using Profiles and Resource Filtering

When managing the lifecycle of a Java application, distinguishing configuration between development, testing, and production environments is crucial. Apache Maven facilitates this separation through Build Profiles and Resource Filtering. This mechanism allows developers to toggle environment-specific files or variables dynamically during the build process without altering the source code structure manually.

To initiate a build with a specific environment configuration, you can execute the following command:

mvn clean deploy -DskipTests -P live-environment

In this command, the -P live-environment flag instructs Maven to activate the profile section within the pom.xml that corresponds to the ID live-environment. This activation sets the stage for anvironment-specific property definitions.

Defining the Profile and Properties

Inside the pom.xml, you define a profile to encapsulate the configuration for a specific environment. Within this profile, you can declare properties that will be used later for resource filtering.

<profiles>
    <profile>
        <!-- The ID matches the command line argument -P live-environment -->
        <id>live-environment</id>
        <properties>
            <!-- Define a custom property to be used as a variable -->
            <deployment.stage>prod</deployment.stage>
        </properties>
    </profile>
</profiles>

With this configuration, the deployment.stage property is assigned the value prod whenever the live-environment profile is active.

Configuring Resource Filtering

The next step involves configuring the Maven build process to perform resource filtering. This feature scans your resource files (such as .xml or .properties files) and replaces any placeholders matching the defined properties with their actual values.

<build>
    <!-- Enable filtering for the main resources directory -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <!-- Specify which files should undergo filtering -->
                <include>application-context.xml</include>
            </includes>
        </resource>
    </resources>
    
    <!-- Define external files containing the variable values -->
    <filters>
        <!-- This path constructs the filename based on the active profile property -->
        <filter>src/main/resources/config-${deployment.stage}.properties</filter>
    </filters>
</build>

In the configuration above, the <filtering> element is set to true, enabling the substitution process. The <filters> section defines a properties file path. Because the value of ${deployment.stage} resolves to prod, Maven will look for a file named config-prod.properties in the classpath to load the properties used for substitution.

Practical Application and Benefits

This setup is particularly effective for managing external configurations. For example, a Logback configuration file might contain a placeholder for the log level or file path. By using this strategy, the build process automatically injects the correct values from config-prod.properties in to the application-context.xml or logback.xml.

Furthermore, Maven profiles offer flexibility beyond simple property files. They can be used to manage repository configurations. For instance, a development profile might point to a snapshot repository to allow access to latest unstable versions, while a production profile would strictly reference a release repository to ensure stability and version control.

Tags: Maven build-profiles resource-filtering java continuous-integration

Posted on Wed, 23 Sep 2026 16:28:47 +0000 by richardk1