Maven profiles and parametrizing using them and retrieve those values using Java.

Sameera De Silva
2 min readMar 26, 2024

--

Assume that , based on the Country, you need to pass county code , capital and currency to the Java Code.

For this instance, we could achieve it by creating two profiles for each country.

<profiles>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>australia</id>
<properties>
<country.code>AU</country.code>
<capital>Canberra</capital>
<currency>AUD (Australian Dollar)</currency>
</properties>
</profile>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>newzealand</id>
<properties>
<country.code>NZ</country.code>
<capital>Wellington</capital>
<currency>NZD (New Zealand Dollar)</currency>
</properties>
</profile>
</profiles>

Properties that in Maven profile, won’t pass to system properties.

To pass the properties defined in Maven profiles as system properties when running tests,
you can use the maven-surefire-plugin to set system properties during the test execution phase.

  <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version> <!-- Adjust version as needed -->
<configuration>
<systemPropertyVariables>
<country.code>${country.code}</country.code>
<capital>${capital}</capital>
<currency>${currency}</currency>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

So final pom.xml will be like this below.

<?xml version="1.0" encoding="UTF-8"?>
<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>org.country</groupId>
<artifactId>CountryProfileMavenDemo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>australia</id>
<properties>
<country.code>AU</country.code>
<capital>Canberra</capital>
<currency>AUD (Australian Dollar)</currency>
</properties>
</profile>
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>newzealand</id>
<properties>
<country.code>NZ</country.code>
<capital>Wellington</capital>
<currency>NZD (New Zealand Dollar)</currency>
</properties>
</profile>
</profiles>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version> <!-- Adjust version as needed -->
<configuration>
<systemPropertyVariables>
<country.code>${country.code}</country.code>
<capital>${capital}</capital>
<currency>${currency}</currency>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>

How to switch between Maven profiles? Select only 1 profile from here and click “reload all maven projects” icon.

So using this Java code and selecting Maven profile from IntelliJ , we could
select the values for each profile.

package codes;

import org.testng.annotations.Test;

public class EnvironmentProfile {

/*
Based on the active Maven profile, Maven will set system properties (country.code, currency, and capital) accordingly.
So we don't need conditional statements.

mvn clean install -P australia test

The command mvn package -P australia tells Maven to build a distributable package for your project. It activates the "australia" profile in your pom.xml file, which might include environment-specific settings or dependencies.
This allows you to create customized builds for different scena
mvn package -P australia

This command cleans the project, activates the "australia" profile,
builds the project, installs the artifact into the local repository,
and then runs all tests in the project.

To see which profile is active
mvn help:active-profiles

*/
@Test
public void testPrintEnvironmentDetails() {
System.out.println("Country Code:" + System.getProperty("country.code"));
System.out.println("Capital City: " + System.getProperty("capital"));
String code=System.getProperty("capital");
System.out.println("Currency: " + System.getProperty("currency"));
}
}

Output for Australia profile.

Country Code:AU
Capital City: Canberra
Currency: AUD (Australian Dollar)

Output for New Zeeland profile.

Country Code:NZ
Capital City: Wellington
Currency: NZD (New Zealand Dollar)

--

--

Responses (1)