Open In App

What are Maven Profiles?

Last Updated : 27 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Maven Profiles allow you to define alternative build configurations directly within a single pom.xml file. Essentially, each profile can override or introduce specific settings, such as different dependencies, plugins, or properties.

Profiles are like switches in that you can turn them on manually or automatically based on conditions like environment variables, operating systems, or JDK versions. This flexibility is ideal for projects that need different configurations for different contexts.

Why Use Maven Profiles?

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.

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

Steps of Creating a Maven Profile

Here we will see how to create and use Maven Profiles in a Selenium WebDriver project, building on your previous queries about Selenium and TestNG.

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.

XML
<project 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/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:

XML
<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

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:

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-tests to skip tests.
  • mvn test -Pintegration-tests to run integration tests only.
  • mvn test -Pmutation-tests to 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-environment profile can be activated using mvn test -Denv=dev, which configures the test to use a development database URL.
  • The linux-build profile 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:

output-of-Maven-Profile
Output of Maven Profile

Profiles Explanation

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.url property based on the env system 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.

Best Practices for Maven Profiles

When using Maven profiles, it's important to follow certain best practices to ensure smooth project management and avoid unnecessary complexity. Here are some tips for using Maven profiles effectively:

  • Keep Profiles Focused: Each profile should address a specific use case, such as development, testing, or production. Avoid combining multiple tasks in a single profile to reduce confusion and maintain clarity.
  • Use Descriptive IDs: Give your profiles clear and meaningful names like dev, prod, or ci that help you easily identify the purpose of the profile. This makes it easier for others to understand the build process.
  • Document Profiles: Document each profile in the pom.xml file or maintain separate project documentation to explain its purpose. This helps other developers understand why specific profiles exist and how to use them properly.
  • Test Profile Combinations: If your project requires the use of multiple profiles, make sure they can work together without conflicts. For large projects, test combinations of profiles to ensure compatibility.
  • Leverage Automatic Activation: Use conditions such as <activation> to automate profile activation. This reduces the need for manually specifying profiles during builds, streamlining the process.
  • Avoid Overuse: While profiles are powerful, too many can clutter the pom.xml file. Use them judiciously, and consider simpler alternatives like properties or build plugins for minor variations in builds. This ensures your pom.xml remains manageable and easy to understand

Maven is used for managing both small projects and complex microservices architectures, Maven profiles significantly enhance productivity and maintainability. By defining different profiles in the pom.xml file, you can easily switch between various settings tailored to specific environments (e.g., development, testing, or production). This flexibility allows teams to adapt their build process to different platforms and scenarios with minimal effort.


Next Article
Article Tags :

Similar Reads