FinalFive
FinalFive

Reputation: 1485

Passing system properties from command line to my app via gradle

My app needs a location for testing. I would like to pass this is in via a system property.

When running on the command line, gradle does not pass on the value:

./gradlew -Dfoo=bar clean cucumber

The Java code:

System.out.println("**foo(props):"+ System.getProperty("foo"));

The output:

**foo(props):null

I've seen some docs about this here, but when I try and use that in my gradle script I get the following error:

Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.

Deprecated dynamic property: "systemProperties" on "task ':cucumber'", value: "{jna.platform.library....".

Here's a snippet of my build.gradle:

task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        systemProperties = System.getProperties()
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['-f', 'pretty', '--glue', 'steps', 'src/test/resources']
        }
    }
}

How do you pass the system property to the app via Gradle?

Upvotes: 3

Views: 4986

Answers (1)

Peter Niederwieser
Peter Niederwieser

Reputation: 123910

systemProperties needs to go inside javaexec:

task cucumber(type: JavaExec) {
    dependsOn assemble, compileTestJava
    main = "cucumber.api.cli.Main"
    classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
    args = ['-f', 'pretty', '--glue', 'steps', 'src/test/resources']
    systemProperties = System.getProperties()
}

PS: Unless you absolutely need to combine multiple concerns into a single task, always prefer task types (e.g. JavaExec) over methods (e.g. javaexec).

Upvotes: 5

Related Questions