Learn about how to Build Profiles in Maven

0
2735
Build Profiles

In the last article, we discussed about the Maven build life cycle in detail. In this article, we are going discuss about the types of Maven build profiles in detail. A build profile in Maven is a set of configurable values that can be applied to set or override the default values of the Maven build. A build profile in Maven helps customize the Maven build such as build for SIT (System Integration Test), UAT (User Acceptance Test), production, etc.

A build profile is specified in pom.xml file with the use of its activeProfiles and profiles elements which can be triggered in number of ways. Various build profiles present in POM at build time provide parameters applicable to different target environments (e.g. we use different databases for development, SIT, UAT and production environments which can be easily switched using Maven build profiles).

Types of Build Profile in Maven

Maven has the following three types of Build profiles.

S No.

Build Type

Description

1.

Per Project level

This build profile is defined in the project POM file (i.e. pom.xml) at per project level.

2.

Per User level

This build profile is defined in Maven settings xml file at user level i.e. at %USER_HOME%/.m2/settings.xml location on the user’s system.

3.

Global level

This build profile is defined in Maven global settings xml file i.e. at %M2_HOME%/conf/settings.xml location where Maven plugin is installed.

Activation of Build Profile in Maven

The following are the number of ways to activate a Maven build profile.
• Use of command on the input console explicitly.
• Through maven settings.
• Through the environment variables i.e. User or System variables.
• OS Settings e.g. Windows, Linux, UNIX, etc.
• Through properties or set up files.

Example on Maven Build Profile Activation
In the Maven project, under src/main/resources, the following are the three environment specific files i.e. environment.properties, environment.sit.properties, environment.uat.properties and environment.prod.properties.

S No.

Build Type

Description

1.

environment.properties

The default configuration which will be activated to use when no profile is mentioned.

2.

environment.sit.properties

The SIT (System Integration Test environment) configuration which will be activated to use when SIT profile is mentioned.

3.

environment.uat.properties

The UAT (User Acceptance Test environment) configuration which will be activated to use when UAT profile is mentioned.

4.

environment.prod.properties

The Production configuration which will be activated to use when Production profile is mentioned.

The following will be the typical file structure of a Maven project which is using various build profiles.
Maven Project Structure
1. Explicit Profile Activation: – In explicit profile activation, an environment profile is activated while executing the pom.xml through Maven. Here in the pom.xml file, we have attached maven-antrun-plugin: run goal to test the phase which will allow us to print the given text messages for different profiles chosen for execution. We will be using pom.xml to define different profiles as default, sit, uat and prod and will activate profile at eclipse console which executing it as Run as  Maven build.. as shown below.
Explicit Profile ActivationThe profile ‘uat’ will be successfully executed which will print the message that we have given under the task section in pom.xml.

The following is the content of the pom.xml file.

Output:
when we build the above Maven project with profile id as ‘uat’, then we can observe the following output log with the task printing the message as ‘[echo] Using environment.uat.properties’.

2. Profile Activation through Maven Settings: – In this type of profile action through Maven settings, the Maven settings.xml file which is available at %USER_HOME%/.m2 directory on your system is updated with the required profile activation details as shown below. Here, %USER_HOME% is nothing but the user home directory.

The following are the content of settings.xml

In this case, we need not to provide the profile id explicitly while building a Maven build, but it is automatically transfered to pom.xml during the build time. We can observe the same output logs as before since we have activated profile id as ‘uat’.

3. Profile Activation through Environment Variables: – In this case, we provide the profile activation details through environment variables. Therefore, remove the profile settings from maven’s settings.xml and modify the ‘test’ profile in pom.xml as shown below. The ‘test’ profile will pick up the system property “env” which has the value as “uat”. When this pom.xml is executed it will provide the same output as we got in above two approaches.

Source code for building different types of Maven profiles

Conclusion: –
In this article, we discussed about building different types of Maven profiles along with a suitable example for each of the type.

LEAVE A REPLY

Please enter your comment!
Please enter your name here