How to give System property to my test via Gradle and -D

gradle, java, testing

Solution

The -P flag is for gradle properties, and the -D flag is for JVM properties. Because the test may be forked in a new JVM, the -D argument passed to gradle will not be propagated to the test - it sounds like that is the behavior you are seeing.

You can use the systemProperty in your `test` block as you have done but base it on the incoming gradle property by passing it with it -P:

test {
    systemProperty "cassandra.ip", project.getProperty("cassandra.ip")
}

or alternatively, if you are passing it in via -D

test {
    systemProperty "cassandra.ip", System.getProperty("cassandra.ip")
}

Problem

I have a a Java program which reads a System property ``` System.getProperty("cassandra.ip"); ``` and I have a Gradle build file that I start with ``` gradle test -Pcassandra.ip=192.168.33.13 ``` or ``` gradle test -Dcassandra.ip=192.168.33.13 ``` however System.getProperty will always return null. The only way I found was to add that in my Gradle build file via ``` test { systemProperty "cassandra.ip", "192.168.33.13" } ``` How Do I do it via -D

Original source

Related problems