Home Maven Maven - How to set System Properties?

Maven – How to set System Properties?

This is a quick guide on how to set System Properties with Maven to the tests and while executing Java Main class with Maven.

We can set System Properties with maven in several ways, either using maven plugin or from command-line or from a file.

1. From Command line

To provide System Properties to the tests from command line, you just need to configure maven surefire plugin and use -D{systemproperty}={propertyvalue} parameter in commandline.

	<build>
		<plugins>
			<!-- plugin to run test cases from maven -->
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M5</version>
			</plugin>
		</plugins>
	</build>

Test Case:

public class MessageUtilTest {

	@Test
	void msg_add_test() {
		String msg = System.getProperty("my_message");
		assertEquals(msg, "Hello, Developer!");
	}
}

Run Single Test with Maven :

$ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Developer!"

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.util.MessageUtilTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.437 s - in com.javabydeveloper.util.MessageUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  24.123 s
[INFO] Finished at: 2021-01-18T11:41:12+08:00
[INFO] ------------------------------------------------------------------------

2. Set System properties with Surefire Plugin

2.1. In surefire plugin configuration, we can set System properties using either systemPropertyVariables or systemProperties (systemProperties deprecated in latest plugin version) configuration parameters.

	<build>
		<plugins>
			<!-- plugin to run test cases from maven -->
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>3.0.0-M5</version>
				<configuration>
					<systemPropertyVariables>
						<my_message>Hello, Developer!</my_message>
					</systemPropertyVariables>
				</configuration>
			</plugin>
		</plugins>
	</build>

2.2. The System properties configured in surefire plugin can be override from command-line with -D flag.

$ mvn test -Dtest=MessageUtilTest#msg_add_test -Dmy_message="Hello, Java Developer!"

2.3. If you have large number of properties to set, you can define System Properties in a properties file also using systemPropertiesFile option, and we need to provide path of file.

			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>${maven-surefire-plugin.version}</version>
				<configuration>
					<systemPropertiesFile>src/test/resources/system_props.properties</systemPropertiesFile>
				</configuration>
			</plugin>

system_props.properties file:

my_message1=Hello,
my_message2=Dear
my_message3=Developer!

Test Case:

public class MessageUtilTest {

	@Test 
	void msg_add_test2() {
		String msg1 = System.getProperty("my_message1");
		String msg2 = System.getProperty("my_message2");
		String msg3 = System.getProperty("my_message3");
		String msg = msg1+" "+msg2+" "+msg3;

		assertEquals(msg, "Hello, Dear Developer!");
	}
}

Results:

$ mvn test -Dtest=MessageUtilTest#msg_add_test2


[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.util.MessageUtilTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.342 s - in com.javabydeveloper.util.MessageUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  20.983 s

2.4 The System Properties defined in file also can be override from command-line using -D flag. Let’s see an example.

$ mvn test -Dtest=MessageUtilTest#msg_add_test2 -Dmy_message3=Customer!

3. Set System Properties to Integration Tests

We use Maven Failsafe plugin to execute integration tests. Like surefire plugin, failsafe plugin also provides same configuration parameters to set System Properties. You can apply all above methods to the failsafe plugin as well.

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-failsafe-plugin</artifactId>
				<version>3.0.0-M5</version>
				<executions>
					<execution>
						<goals>
							<goal>integration-test</goal>
							<goal>verify</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<systemPropertyVariables>
						<my_message>Hello, Developer!</my_message>
					</systemPropertyVariables>
					<systemPropertiesFile>src/test/resources/system_props.properties</systemPropertiesFile>
				</configuration>
			</plugin>

Executing Integration Test:

public class ITMessageUtilTest {

	@Test 
	void msg_add_test2() {
		String msg1 = System.getProperty("my_message1");
		String msg2 = System.getProperty("my_message2");
		String msg3 = System.getProperty("my_message3");
		String msg = msg1+" "+msg2+" "+msg3;

		assertEquals(msg, "Hello, Dear Developer!");
	}
}
$ mvn verify -Dit.test=ITMessageUtilTest#msg_add_test2

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.javabydeveloper.util.ITMessageUtilTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.357 s - in com.javabydeveloper.util.ITMessageUtilTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-failsafe-plugin:3.0.0-M5:verify (default) @ Junit5-maven-example ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  32.124 s


4. Using Exec plugin

4.1. Exec plugin will be used to run Java Main class with Maven. Let’s see the configuration to set System Properties from command-line and in plugin configuration.

MainClass1:

public class MainClass1 {

	public static void main(String[] args) {
		
		System.out.println("---- Main Class1 -----");
		System.out.println("my_prop: "+System.getProperty("my_prop"));
	}
}

Plugin Configuration:

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>3.0.0</version>
			</plugin>

Results:

$ mvn exec:java -Dexec.mainClass="com.javabydeveloper.util.MainClass1" -Dmy_prop="Hello, Developer!"


[INFO]
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ Junit5-maven-example ---
---- Main Class1 -----
my_prop: Hello, Developer!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.385 s

4.2. You can configure System Properties at plugin level and can specify specific phase.

			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>3.0.0</version>
				 <executions>
					<execution>
						<phase>test</phase>
						<goals>
							<goal>java</goal>
						</goals>
						<configuration>
							<mainClass>com.javabydeveloper.util.MainClass1</mainClass>
							<systemProperties>
								<systemProperty>
									<key>my_prop</key>
									<value>Hello, Dear Developer!</value>
								</systemProperty>
							</systemProperties>
						</configuration>
					</execution> 
				</executions>
			</plugin> 

Results:

$ mvn test

[INFO]
[INFO] --- exec-maven-plugin:3.0.0:java (default) @ Junit5-maven-example ---
---- Main Class1 -----
my_prop: Hello, Dear Developer!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  19.563 s

5. Conclusion

In this tutorial, we discussed on how to set System Properties with Maven to the tests and while executing Java Main class with Maven.

You also might interested in following Maven guides:

  1. Install Maven on Windows
  2. Install Maven on Mac
  3. Install Maven on Ubuntu
  4. Import maven project into Eclipse
  5. Maven run single test
  6. Maven set java version
  7. Maven skip tests
  8. Maven local repository

6. References

  1. Integration Tests
  2. Surefire plugin

LEAVE A REPLY

Please enter your comment!
Please enter your name here