Maven Profiles prove incredibly useful in scenarios where your project's build process needs to adapt to varying conditions without changing the core pom.xml for each case. They provide a clean and efficient way to manage these variations.
Use Case:
- Switch Environments: Utilize distinct database connections or API keys for development, testing, and production environments.
- Customize Dependencies: Include libraries only for specific builds, like a testing framework for CI pipelines.
- Adapt to Platforms: Adjust settings for Windows, Linux, or macOS.
- Control Build Behavior: Enable plugins or goals selectively, such as generating reports only during CI builds.
Here we will see Steps of Creating a Maven Profile
Step 1: Set Up a Maven Project
Create a Maven project with dependencies for Selenium, TestNG, and WebDriverManager, as shown in your earlier pom.xml file.
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>selenium-automation</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<selenium.version>4.26.0</selenium.version>
<testng.version>7.10.2</testng.version>
<webdrivermanager.version>5.9.2</webdrivermanager.version>
<surefire.version>3.5.0</surefire.version>
<pitest.version>1.16.0</pitest.version>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
Step 2: Add Profiles to pom.xml
Add profiles to your pom.xml file to handle different testing scenarios. For example, the no-tests profile skips all tests to speed up the build process. The integration-tests profile runs only integration tests. The mutation-tests profile executes mutation testing using PITest to check the effectiveness of tests. Finally, the dev-environment profile configures a specific database URL for the development environment. These profiles help streamline the testing process based on different needs.
Update the Profiles in the pom.xml file:
<profiles>
<!-- Profile to skip tests -->
<profile>
<id>no-tests</id>
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
<!-- Profile for integration tests -->
<profile>
<id>integration-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<!-- Profile for mutation testing -->
<profile>
<id>mutation-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<executions>
<execution>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Profile for development environment -->
<profile>
<id>dev-environment</id>
<activation>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<db.url>jdbc:mysql://localhost:3306/devdb</db.url>
</properties>
</profile>
<!-- Profile for production environment -->
<profile>
<id>prod-environment</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
<properties>
<db.url>jdbc:mysql://prod-server:3306/proddb</db.url>
</properties>
</profile>
<!-- Profile for Linux builds -->
<profile>
<id>linux-build</id>
<activation>
<os>
<family>unix</family>
<name>linux</name>
</os>
</activation>
<properties>
<build.platform>linux</build.platform>
</properties>
</profile>
</profiles>
Step 3: Example Test Class
Create an example test class to demonstrate how to use profiles in your project. Here's a sample TestNG test class, DatabaseTest.java, that fetches the database URL based on the active profile. By leveraging Maven profiles, the test class dynamically adapts to different environments (like development or production) based on the configured profile in pom.xml. This allows for flexible testing without needing to manually change environment settings.
The Location of the Class file will be: src/test/java/com/example/DatabaseTest.java
package com.example;
import org.testng.annotations.Test;
import static org.testng.Assert.assertNotNull;
public class DatabaseTest {
@Test
public void testDatabaseConnection() {
// Access the db.url property defined in the active profile
String dbUrl = System.getProperty("db.url", "jdbc:mysql://localhost:3306/testdb");
assertNotNull(dbUrl, "Database URL is not set!");
System.out.println("Using database URL: " + dbUrl);
}
}
Create the TestNg file which name is testng.xml:
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="DatabaseTestSuite">
<test name="DatabaseTest">
<classes>
<class name="com.example.DatabaseTest"/>
</classes>
</test>
</suite>
Step 4: Activating Profiles
Activate Maven profiles, either manually or automatically, to suit different testing or build scenarios.
Manual Activation using commands:
mvn package -Pno-teststo skip tests.mvn test -Pintegration-teststo run integration tests only.mvn test -Pmutation-teststo execute mutation tests using PITest.- You can also combine profiles, like
mvn test -Pintegration-tests,mutation-tests, to run both integration and mutation tests together.
Automatic Activation triggers profiles based on certain conditions:
- For example, the
dev-environmentprofile can be activated usingmvn test -Denv=dev, which configures the test to use a development database URL. - The
linux-buildprofile automatically activates when running on Linux, setting build-related properties.
At the end, you can verify the database URL based on the active profile, running mvn test -Denv=dev for the development environment or mvn test -Denv=prod for production.
Output of testng.xml:

Profiles in Maven provide a way to customize the build process based on specific needs. Here's a breakdown of each profile:
- no-tests: Skips all tests by setting
maven.test.skip=true, which is useful for quicker builds during development when you want to avoid running tests. - integration-tests: Configures the Surefire plugin to only run files that match
*IntegrationTest.java, ideal for running integration tests in continuous integration (CI) environments. - mutation-tests: Uses the PITest plugin to perform mutation testing, helping evaluate the quality of your tests by generating a mutation coverage report.
- dev-environment and prod-environment: These profiles override the
db.urlproperty based on theenvsystem property, ensuring that the correct database URLs are used for development or production environments. - linux-build: Automatically activates when running on a Linux system, setting platform-specific properties for Linux builds.