Getting Started with Maven | Miscellaneous | Separating Configuration Files by Environment

When developing a program, you manage various configuration files. These may include JDBC configuration, logging configuration, and other resources. Such configuration files inevitably have different settings for each stage, such as testing and production. If you had to change configuration files manually every time you package the application, it would quickly become tedious.

You can configure these differently at package build time by using Maven profiles.

Separating Resource Folders by Environment

Create resource folders for each environment under the main source directory, and create configuration files for each environment.

  • src/main/resources-${env}

For example, if you have separate test and production environments, the structure might look like the following.

  • Development test server environment
    • src/main/resources-dev
    • src/main/resources-dev/jdbc.properties
      jdbc.driverClassName=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://127.0.0.1/database
      jdbc.username=root
      jdbc.password=1234
      
  • Actual production server environment
    • src/main/resources-real
    • src/main/resources-real/jdbc.properties
      jdbc.driverClassName=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://192.168.0.1/database
      jdbc.username=admin
      jdbc.password=1111
      

Adding Build Settings to pom.xml

In Maven’s pom.xml, configure the build resources so the path matching the environment is used.

The next step is to add the resource folder for the selected profile to the classpath. By default, src/main/resources is included, and you can add the environment-specific resource folder additionally. Add the following content to the build section of pom.xml.

<build>
    <resources>
         <resource>
            <directory>src/main/resources</directory>
         </resource>
         <resource>
             <directory>src/main/resources-${env}</directory>
        </resource>
     </resources>
</build>

Adding Profile Settings to pom.xml

Now configure Maven profiles so the desired resource folder is added to the classpath during the build phase.

First, add profiles to pom.xml as follows.

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
         </activation>
      <properties>
          <env>dev</env>
      </properties>
    </profile>
    <profile>
        <id>real</id>
        <properties>
            <env>real</env>
        </properties>
    </profile>
</profiles>
  • The <activeByDefault> setting in the dev profile makes it the default profile if no special command is specified when packaging.

Setting the Default Profile in pom.xml

Specify the default profile for Maven. There are two ways to do this.

Add env to properties.

<properties>
    <!-- Default profile when -P is not specified -->
    <env>dev</env>
</properties>

Alternatively, add activation to the profile.

 <profile>
    <id>dev</id>
     <activation>
         <activeByDefault>true</activeByDefault>
      </activation>
      <properties>
          <env>dev</env>
      </properties>
</profile>

If you do not configure this, the configuration files in src/main/resources-${env} are not included in the package.

Packaging with mvn package -P ProfileName

The configured profile can be selected together with the -P option in the Maven command.

mvn clean package -P dev

If you run the mvn package command without adding -P, packaging is performed with the default profile.

Reference