Reputation: 20179
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
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