Let us see how to configure Maven Profile for a Maven Project.

What is a Maven Profile..??                                                               

It is a feature that can be defined in a pom.xml file which allows developers and testers to configure and customize builds based on requirement. 

What is the advantage of implementing Maven Profile in our Automation Projects..??                                                                 

As shown below, In real time we generally maintain different testng.xml files in frameworks that are designed either for Web Applications, Backend APIs or Mobile Applications like Smoke Suite, Regression Suite, Sanity Suite etc. for executing test cases based on requirement.             

Based on business requirement, we will be required to execute testng.xml files from Eclipse IDE, Command Prompt or from Jenkins etc.

In general, if we do not use Maven Profiling. Each time when we need to execute testng.xml file. We need to go to pom.xml file and change the name of testng.xml inside Maven Surefire Plugin which needs to be executed and that is a very tedious task to do every time.  Now the question is, how can we avoid changing testng.xml file name in pom.xml every time. when we need to execute test suite.

Now let us see configuring Maven Profile with Maven Project.

1. Project should be a Maven Project. 

2. Maven Jar files should be downloaded and path must be set for the same in Environment Variables and ensure that you are able to execute Maven Project from Command Line prompt before implementing Maven Profile. 

Refer below blog with respect to above points to Install Maven Jars and to execute Maven Project from Command Prompt:  http://naveenautomationlabs.com/executing-maven-project-in-windows-from-command-prompt/

Below is Maven Surefire Plugin where we mention which testng.xml file needs to be executed from project.

Code for Maven Surefire Plugin:                                                     

<build>
 <pluginManagement>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-surefire-plugin</artifactId>
 <version>2.18.1</version>
 <configuration>
 <suiteXmlFiles>
 <suiteXmlFile>src/main/resources/testng_smoke.xml</suiteXmlFile>
 </suiteXmlFiles>
 </configuration>
 </plugin>
 </plugins>
 </pluginManagement>
 </build>

Now, let us see how can we build Maven Profile using Maven Surefire Plugin.

We can build Maven Profile for each testng.xml file in pom.xml as shown below, for each Maven Profile we can give Id/Name as highlighted in below screenshot.

All we need to add is followed tags with Maven Surefire Plugin <profiles> </profiles>, <profile> </profile> and <id> </id>.

All we need to do is that providing Id/Name for each Profile. For example, if we have multiple testng.xml files like Smoke, Sanity and Regression then we can build 3 Maven Profiles.       

One-One Profile for each suite, so that we can avoid changing testng.xml file name inside Maven Surefire Plugin whenever we need to execute test suite.

In my case, I have 2 testng.xml files. One is for Smoke and other one is for Regression.

You can refer below screenshots for giving Maven Profile Names for Test Suites.

Note: Profile Name should always be written inside <id> tag.

Refer below pom.xml to build Maven Profile for each testng.xml file.    https://github.com/PavanReddy77/MavenSurefirePlugin/blob/master/pom.xml

Once Maven Profiles are built for all testng.xml files then we are good to execute from Command Prompt or from Jenkins.

Maven Commands to be followed when executing testng.xml files. 

mvn test –P followed by Maven Profile Id like below:

mvn test –PSmoke     

mvn test –PRegression

Well, we are now good to execute our testng.xml file based on our requirement.

Refer below repository for implementing Maven Surefire Plugin and Maven Profile.  

https://github.com/PavanReddy77/MavenSurefirePlugin

Conclusion: In this blog, we have seen implementing Maven Surefire Plugin and Maven Profile in a Maven Project.

Blog Contributors:

Author:  Pavan K

Pavan, having good knowledge in Selenium Web UI and API Automation frameworks. Also, he is helping QA community with his knowledge along with Naveen AutomationLabs!!

LinkedIn

Reviewer: Naveen Khunteta 

https://www.linkedin.com/in/naveenkhunteta