Maven Build Profiles - Maven

What is Build Profile?

A Build profile is known as a set of configuration values used to set or override default values. To build profile with Maven, you need to customize build for different environments such as Production v/s Development environments.

While building profiles you need to specify in pom.xml file using its active Profiles/profiles elements and are triggered in different ways. You can modify the POM at build time, and are used to give parameters different target environments like development, testing, and production environments.

What are the types of Build Profile?

Build profiles are majorly of three types.

Type Where it is defined
Per Project Defined in the project POM file, pom.xml
Per User Defined in Maven settings xml file (%USER_HOME%/.m2/settings.xml)
Global Defined in Maven global settings xml file (%M2_HOME%/conf/settings.xml)

Profile Activation

A Maven Build Profile can be executed in various ways.

  • Explicitly using command console input.
  • Through maven settings.
  • Based on environment variables (User/System variables).
  • OS Settings (for example, Windows family).
  • Present/missing files.

Profile Activation Examples

Here’s the following directory structure of your project −

structure

Now, under src/main/resources, there are three environment specific files −

Sr.No. File Name & Description
1

env.properties

default configuration used if no profile is mentioned.

2

env.test.properties

test configuration when test profile is used.

3

env.prod.properties

production configuration when prod profile is used.

Explicit Profile Activation

Explicit Profile Activation is explained in the following example, where we attach maven-antrun-plugin:run goal to test the phase. We can allow to echo text messages for different profiles. For this use pom.xml to define different profiles and will activate profile at command console using maven command.

Assume, we've created the following pom.xml in C:\MVN\project folder.

And assume, we've created following properties file in C:\MVN\project\src\resources folder.

env.properties

env.test.properties

env.prod.properties

Let’s open the command console and go to the folder containing pom.xml and execute the following mvn command. Now pass the profile name as argument using -P option.

Maven will start processing and displaying the result of test build profile.

You can perform the following steps −

  • First add another profile element to profiles element of pom.xml (copy existing profile element and paste it where profile elements ends).
  • Now add id of this profile element from test to normal.
  • Then go to task section to echo env.properties and copy env.properties to target directory.
  • After that repeat the above three steps, update id to prod and task section for env.prod.properties.
  • Now you've three build profiles ready (normal/test/prod).

Let’s open the command console, go to the folder containing pom.xml and execute the following mvn commands. Pass the profile names as argument using -P option.

Check the output of the build to see the difference.

Profile Activation via Maven Settings

To activate the profile open Maven settings.xml file available in %USER_HOME%/.m2 directory where %USER_HOME% represents the user home directory. If settings.xml file is not there, then create a new one.

Then add test profile as an active profile using active Profiles node as shown below in example.

Let’s open command console, go to the folder containing pom.xml and execute the following mvn command. But not pass the profile name using -P option. Then Maven will showcase the result of test profile being an active profile.

Profile Activation via Environment Variables

Here you can remove active profile from maven settings.xml and update the test profile mentioned in pom.xml. You can add activation element to profile element as mentioned below.

The test profile will trigger when the system property "env" is specified with the value "test". Create an environment variable "env" and set its value as "test".

Now open command console, go to the folder containing pom.xml and execute the following mvn command.

Profile Activation via Operating System

You can activate the element to include os detail as mentioned below. This test profile will trigger when the system is windows XP.

Now open command console, go to the folder containing pom.xml and execute the following mvn commands. Do not pass the profile name using -P option. Maven will display result of test profile being an active profile.

Profile Activation via Present/Missing File

To activate via present/missing file can be as shown below. The test profile will trigger when target/generated-sources/axistools/wsdl2java/com/companyname/group is missing.

Go command console and open then go to the folder containing pom.xml and execute the following mvn commands. But never pass the profile name using -P option. Maven will display result of test profile being an active profile.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Maven Topics