Rylander
Rylander

Reputation: 20179

How can I set a Java System Property using the Gradle comand line?

I want to be able to set a system property via the command line so that it can be accessed by java code. How can I achieve this functionality?

The example below fails.

Command Line

./gradlew clean test -Dfoo=bar

Java Code

@Test
public void shouldGetFoo(){
    String myProp = System.getProperty("foo");

    assertThat(myProp, is("bar"));
}

Upvotes: 0

Views: 1945

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123996

build.gradle:

test {
    systemProperty "foo", System.getProperty("foo")
}

This is necessary because tests always run in a separate JVM. All other task types that implement JavaForkOptions (e.g. JavaExec) have the same property. For API details, consult the Gradle Build Language Reference.

Upvotes: 3

Related Questions