Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Learn about how to Build Profiles in Maven

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.

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.

The 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.

4.0.0com.eduonix
	build.profile
	0.0.1-SNAPSHOTbuild.profilehttp://maven.apache.orgsrc/main/resources/${env}.propertiesinstallsrc/main/resourcestrue*.properties*.xmlprojectenvironmentUTF-8sitenvironment.sitorg.apache.maven.plugins
						maven-antrun-plugin
						1.1testrunUsing environment.sit.propertiesuatenvironment.uatorg.apache.maven.plugins
						maven-antrun-plugin
						1.1testrunUsing environment.uat.propertiesprodenvironment.prodorg.apache.maven.plugins
						maven-antrun-plugin
						1.1testrunUsing environment.prod.propertiesliblibtrueignorefalsefile://${project.basedir}/libjunit
			junit
			3.8.1testlog4j
			log4j
			1.2.16

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’.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building build.profile 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ build.profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 4 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ build.profile ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ build.profile ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ build.profile ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ build.profile ---
[INFO] Surefire report directory: C:\work\project\build.profile\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.eduonix.build.profile.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ build.profile ---
[INFO] Executing tasks
     [echo] Using environment.uat.properties
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ build.profile ---
[INFO] Building jar: C:\work\project\build.profile\target\project.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ build.profile ---
[INFO] Installing C:\work\project\build.profile\target\project.jar to C:\Users\Aparajita\.m2\repository\com\eduonix\build.profile\0.0.1-SNAPSHOT\build.profile-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\work\project\build.profile\pom.xml to C:\Users\Aparajita\.m2\repository\com\eduonix\build.profile\0.0.1-SNAPSHOT\build.profile-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.598 s
[INFO] Finished at: 2017-09-03T12:15:53-04:00
[INFO] Final Memory: 12M/173M
[INFO] ------------------------------------------------------------------------

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

maven.dev.snaponglobal.comInternal artifact Maven repositoryhttp://repo1.maven.org/maven2/*
   
      uat
   

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.

testenvuat

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.

The post Learn about how to Build Profiles in Maven appeared first on Eduonix.com | Blog.



This post first appeared on How And When Should You Use HBase NoSQL DB, please read the originial post: here

Share the post

Learn about how to Build Profiles in Maven

×

Subscribe to How And When Should You Use Hbase Nosql Db

Get updates delivered right to your inbox!

Thank you for your subscription

×