Include different properties files based on profile in Maven

author's avatar
Kees de Kooter
Jun 4 9:52 • 1 min read

The following solutions enables you to package your project for different environments, like develop / test / staging / live.

The profile definition:

<profiles>
  <profile>
    <id>develop</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
      <profile.name>develop</profile.name>
    </properties>
  </profile>
  <profile>
    <id>windows</id>
    <properties>
      <profile.name>live</profile.name>
    </properties>
  </profile>
</profiles>

In the project you create a subdirectory in src/main/profiles for every profile. In that subdir you can put configuration files that are specific for the targeted environment. Mine usually contain a log4j.xml and a deploy.properties that my Spring configuration uses (for more on this see Use PropertyOverrrideConfigurer to target different deployment environments).

Next we add resources to the build. Note the ${profile.name} parameter.

<build>
  <resources>
    <resource>
      <directory>src/main/profiles/${profile.name}</directory>
      <includes>
        <include>*.xml</include>
        <include>*.properties</include>
      </includes>
    </resource>

To use a specific profile in maven you use the -P parameter:

mvn package -Pdevelop