你是什么意思“它返回默认值并且不考虑命令行参数”?以下工作如我所料:
def props = project.properties.findAll { k, v -> k.startsWith("test_") }
.collectEntries { k, v -> [k - "test_", v] }
tasks.register("test") {
doLast {
props.each { println it }
}
}
然后你可以得到预期的输出:
➜ gradle test -Ptest_foo=yay -Ptest_bar=baz
> Task :test
foo=yay
bar=baz
你看到的问题在哪里?这些是否应该传递给问题中省略的其他内容?
更新
好的,所以给定这个测试类src/main/java/test:
package test;
public class Main {
public static void main(String... args) {
System.out.println(System.getProperty("tim", "nope"));
}
}
如果存在,则打印系统属性“tim”的值(如果丢失,则打印“nope”)
然后我们可以使用这个构建脚本:
plugins {
id "java"
id "application"
}
def props = project.properties.findAll { k, v -> k.startsWith("test_") }
.collectEntries { k, v -> [k - "test_", v] }
mainClassName = "test.Main"
tasks.withType(JavaExec).named("run").configure { t ->
props.each { k, v ->
t.systemProperty k, v
}
}
配置插件创建的默认run任务application
然后当我们没有财产时,我们得到:
➜ gradle run
> Task :run
nope
BUILD SUCCESSFUL in 630ms
当我们设置一个属性时,它会被类看到:
➜ gradle run -Ptest_tim=yay
> Task :run
yay
BUILD SUCCESSFUL in 552ms
希望这可以帮助 :-)