Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing system properties from command line to my app via gradle

Tags:

java

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?

like image 271
FinalFive Avatar asked Jan 30 '26 03:01

FinalFive


1 Answers

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).

like image 156
Peter Niederwieser Avatar answered Feb 01 '26 15:02

Peter Niederwieser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!