In this article you will see a java gradle example project, to understand how to set system properties and accessing them via command line and from gradle.properties file.
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
- Via command line.
- 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.
Technologies used:
- Gradle 6.1.1
- Java 8
- Spring Tool Suite 3.9.8
build.gradle:
In build.gradle
, you can get system properties using System.getProperty() and later you can set them to variables.
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.
4. Reference
- Gradle document
- Java System Properties propagation to tests issue
- Create gradle project in eclipse.
- Junit 5 + gradle
Also might be interested in our following tutorials :