What is Maven Build Profiles?

Maven Build Profiles

Maven Profiles: Profile is a subset of elements or configuration value which allows to customize builds for environment such as production or development environments.

Profiles are defined in pom.xml file using its activeProfiles/profiles elements. Profiles modify the Pom at build time and are used to give parameters different target environments (i.e. to configure separate environments for development and production instances. Based on the parameters passed, the corresponding profiles are activated accordingly.)

Types of Build Profile : There are three types of build profile.

  1. Per Project :This is defined in pom.xml.
  2. Per User/Developer :Maven settings.xml (%USER_HOME%/.m2/settings.xml).
  3. Global :Maven global settings.xml (%M2_HOME%/conf/settings.xml).

 

Activating Profiles: The following are the number of ways to activate the build profile.

  1. Use of command on the input console explicitly.
  2. Through maven settings
  3. Through the environment variables
  4. OS settings i.e. windows, linux, Unix etc.
  5. Through properties or setup files.

1. Explicit Profile Activation:  In explicitly profile activation, we attached the maven-antrun-plugin: run goal to test the phase which will allow us to print the given text messages to different profiles chosen for execution. We will be using pom.xml file 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.

<profiles>
      <profile>
      <id>test</id>
      <build>
      <plugins>
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
               <execution>
                  <phase>test</phase>
                  <goals>
                     <goal>run</goal>
                  </goals>
                  <configuration>
                  <tasks>
                     <echo>Using app.test.properties</echo>
                     <copy file="src/main/resources/app.test.properties"
                        tofile="${project.build.outputDirectory}/env.properties"/>
                  </tasks>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
      </build>
      </profile>

 

2. Profile Activation through Maven Settings: In this we will do some changes in maven settings.xml file which is available at %USER_HOME%/.m2 directory on your system is updated. If settins.xml file is not there, then create a new one.

We can add this using active profiles node as shown below.

<settings 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/settings-1.0.0.xsd">
   <mirrors>
      <mirror>
         <id>maven.dev.snaponglobal.com</id>
         <name>Internal Artifactory Maven repository</name>
         <url>http://repo1.maven.org/maven2/</url>
         <mirrorOf>*</mirrorOf>
      </mirror>
   </mirrors>
   <activeProfiles>
      <activeProfile>test</activeProfile>
   </activeProfiles>
</settings>

3. Profile Activation through Environment Variables: In this case, we provide profile activation details through environment variables. Therefore, we need to remove setting from maven settings.xml file and update the test profile mentioned in pom.xml. Add activation element to profile element as shown below. The ‘test’ profile will pick up the system property ‘env’ which has the value as ‘uat’.

<profile>
   <id>test</id>
   <activation>
      <property>
         <name>env</name>
         <value>uat</value>
      </property>
   </activation>
</profile>

4. Profile Activation through Operating System: In this case, activation element include OS details. This test profile is triggered when the system is Linux.

<profile>
   <id>test</id>
   <activation>
      <os>
         <name>Linux</name>
         <family>Linux</family>
         <arch>x86</arch>
         <version>5.1.2600</version>
      </os>
   </activation>

I hope this tutorial will be helpful for you to learn the maven profiles.

 

 

 

 

Leave a Comment