通过 gradle 将系统属性从命令行传递到我的应用程序

Fin*_*ive 3 java gradle

我的应用程序需要一个位置进行测试。我想通过系统属性传递它。

在命令行上运行时,gradle 不会传递值:

./gradlew -Dfoo=bar clean cucumber
Run Code Online (Sandbox Code Playgroud)

Java代码:

System.out.println("**foo(props):"+ System.getProperty("foo"));
Run Code Online (Sandbox Code Playgroud)

输出:

**foo(props):null
Run Code Online (Sandbox Code Playgroud)

我在这里看到了一些关于这个的文档,但是当我尝试在我的 gradle 脚本中使用它时,我收到以下错误:

按需创建属性(又名动态属性)已被弃用,并计划在 Gradle 2.0 中删除。请阅读http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html以获取有关替换动态属性的信息。

不推荐使用的动态属性:“任务 ':cucumber'”上的“systemProperties”,值:“{jna.platform.library....”。

这是我的 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']
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

你如何通过 Gradle 将系统属性传递给应用程序?

Pet*_*ser 5

systemProperties需要进去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()
}
Run Code Online (Sandbox Code Playgroud)

PS:除非您绝对需要将多个关注点组合到一个任务中,否则总是更喜欢任务类型(例如JavaExec)而不是方法(例如javaexec)。