Maven Tutorial - Maven Build Profile








Maven build profiles is a set of configuration values which allows us to build our project using different configurations.

We can use Maven build profile to set or override default values of Maven build.

We can use build profile to customize build for different environments such as production vs test environments.

We can set different database connection URL for testing and production environments.

Profiles are specified in pom.xml file using its profiles elements and are triggered in variety of ways.

Example

<project ...

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.company.fee</groupId>
  <artifactId>fee-calculation</artifactId>
  <version>1.0.0</version>

  <profiles>
      <profile>
          <id>test</id>
          <activation>...</activation>
          <build>...</build>
          <modules>...</modules>
          <repositories>...</repositories>
          <pluginRepositories>...</pluginRepositories>
          <dependencies>...</dependencies>
          <reporting>...</reporting>
          <dependencyManagement>...</dependencyManagement>
          <distributionManagement>...</distributionManagement>
      </profile>
  </profiles>

</project>




Note

A build profile overrides the settings in the POM file when executing under that build profile.

The elements inside the profile element will override the values further up in the POM.

The activation element inside the profile element describes the condition to trigger this build profile to be used.

One way to choose the profile is in the settings.xml file. We can set the active profile there.

Another way is to add -P profile-name to the Maven command line.

Activation

Activations are the key of a profile.

An activation element specifies to condition to modify the basic POM. Here is an example.

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  ...
  <profiles>
    <profile>
      <id>test</id>
      <activation>
        <activeByDefault>false</activeByDefault>
        <jdk>1.8</jdk>
        <os>
          <name>Windows XP</name>
          <family>Windows</family>
          <arch>x86</arch>
          <version>5.1.2600</version>
        </os>
        <property>
          <name>your-type</name>
          <value>yourValue</value>
        </property>
        <file>
          <exists>${basedir}/file2.properties</exists>
          <missing>${basedir}/file1.properties</missing>
        </file>
      </activation>
      ...
    </profile>
  </profiles>
</project>

Activation occurs when one or more of the specified criteria have been met.

When the first positive condition is encountered, processing stops and the profile is marked as active.

ItemDescription
jdkactivation has a built in, Java-centric check in the jdk element. This will activate the profile if the test is run under a jdk version number that matches the prefix given.
osThe os element can define some operating system specific properties.
propertyThe profile will activate if Maven detects a property of the corresponding name=value pair.
fileA given filename may activate the profile by the existence of a file, or if it is missing.

To see which profile will activate in a certain build, use the maven-help-plugin.

mvn help:active-profiles