Home gradle Gradle System properties via command line step by step example

Gradle System properties via command line step by step example

In this article you will see a java gradle example project with gradle system properties and accessing them via command line and from gradle properties.

Following java program that takes System property mymessage from command line. Run following program from command line using

$ java -Dmymessage="Hello Developer!" AMainClass

public class AMainClass {

	public static void main(String[] args) {
		
		// Get the System property value that provided in command line with -Dmymessage
		String message = System.getProperty("mymessage");
		
		System.out.println("Got message from command line : "+ message);
	}
}

You will get following output.

Got message from command line : Hello Developer!

1. Gradle System Properties

Similarly like above program using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command.

There are 2 ways you can provide System Properties to gradle

  1. Via command line.
  2. Using gradle.properties

In following example you will see both the ways. Following example is a basic java gradle project to create, you can refer article Create Gradle project in eclipse.

gradle system properties via command line

Technologies used :

  1. Gradle 6.1.1
  2. Java 8
  3. Spring Tool Suite 3.9.8

build.gradle

plugins {
    id 'java-library'
}

repositories {
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1' 

    implementation 'com.google.guava:guava:28.1-jre'

    testImplementation 'junit:junit:4.12' 
}

test{
    println "Starting Tests"
    println System.getProperty("mymessage") ? System.getProperty("mymessage") : 'no message'
}

1.1. System Properties via command line

Navigate to project context root in command line and run gradle task using following command.

$ gradle clean test -Dmymessage='Hello developer, you are awesome!'

You will see following output in console.

> Configure project :
Starting Tests
Hello developer, you are awesome!

BUILD SUCCESSFUL in 7s
4 actionable tasks: 4 executed

1.2. Using gradle.properties file

You can also set system properties in gradle.properties files with the prefix systemProp. In a multi project build, “systemProp.” properties set in any project except the root will be ignored. That is, only the root project’s gradle.properties file will be checked for properties that begin with the “systemProp.” prefix.

gradle.properties

systemProp.mymessage = 'Hello, I am from gradle.properties'

Navigate to project context root in command line and run gradle task using following command. you don’t pass the -Dmymessage in command line, gradle obtain from gradle.properties file.

$ gradle clean test

You will see following output in console.

> Configure project :
Starting Tests
'Hello, I am from gradle.properties'

BUILD SUCCESSFUL in 5s
4 actionable tasks: 4 executed

2. Accessing System Properties in test cases

public class LibraryTest {
    @Test 
    public void testSomeLibraryMethod() {
    	
    	System.out.println(System.getProperty("mymessage"));
    	
        Library classUnderTest = new Library();
        assertTrue("someLibraryMethod should return 'true'", classUnderTest.someLibraryMethod());
    }
}

By default I couldn’t able to access the System Properties in test classes either provided by command line or gradle.properties. To make it work I have updated test task in build.gradle like following.

test{
    println "Starting Tests"
    println System.getProperty("mymessage") ? System.getProperty("mymessage") : 'no message'

    systemProperties = System.properties
}

3. Conclusion

In this article we have seen a java gradle example project with gradle system properties and accessing them via command line and from gradle properties.

Reference

  1. Gradle document
  2. Java System Properties propagation to tests issue
  3. Create gradle project in eclipse.
  4. Junit 5 gradle configuration example.

Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories