如何以智能方式将系统属性传递给gradle中的测试?

时间:2016-06-30 14:25:06

标签: gradle system-properties

的build.gradle

tasks.withType(Test){
    systemProperties=System.properties
    println systemProperties['param']
}

现在我可以在命令行中传递参数:

gradle test -Dparam=10

或将它们放入 gradle.properties

systemProp.param=15

理想情况下,我想将默认值放在 gradle.properties 中,并能够从命令行覆盖它们。不幸的是,如果我这样做,gradle.properties具有优先权,并忽略-Dparam=10

你能提供任何解决方案吗?

2 个答案:

答案 0 :(得分:1)

https://issues.gradle.org/browse/GRADLE-2122

它自2.12或2.13开始工作"智能方式"已经!

上面的示例正常,命令行-D选项会过载 gradle.properties

中的默认值

答案 1 :(得分:0)

我正在使用gradle 2.12并分享我如何使用它:

test {
  // support passing -Dsystem.property=value to bootRun task
  systemProperties = System.properties
}

除非使用属性包含此类测试,否则我要跳过JUnit测试。使用JUnit Assume有条件地包含测试:

//first line of test
assumeThat(Boolean.parseBoolean(System.getProperty("deep.test.run","false"),true)

使用gradle执行此操作需要在运行gradle build时提供的系统属性,如此处所示

gradle build -Ddeep.test.run=true

确实通过了测试。

希望这有助于其他人尝试这种方法来有条件地运行测试。