这应该是最简单的问题,但我在从Gradle测试任务访问System变量时遇到问题.由于我确信这种语法是正确的但它不起作用,因此我必须要做一个错字.我希望有人可以帮我识别下面这段代码的问题?
// my gradle build says the following line is a deprecated method
//systemProperties = System.getProperties()
// this line always returns 1 on a multiprocessor system
println 'NUMBER_OF_PROCESSORS is ' +
System.getProperty( "NUMBER_OF_PROCESSORS", "1" )
// this line also always returns the default for TMP var
println 'TMP is ' + System.getProperty( "TMP", "C:\\Temp" )
Run Code Online (Sandbox Code Playgroud)
注意:我也在这里问过这个问题,但由于它是一个封闭的线程,我不确定我是否会在那里得到答案.此外,我已经彻底阅读了该文档,但它没有帮助.
我试过这些,但也失败了:
test {
println ""
ENV = System.getProperties()
println "TMP is " + ENV['TMP']
println ""
}
task testa(Type:Test) {
println ""
println "HOMEPATH = " + System.getProperty( "HOMEPATH", "defaultpath" )
println "TMP = " + System.getProperty( "TMP", "defaulttmp" )
println ""
}
task testb(Type:Test) {
println ""
println "HOMEPATH = " + System.properties['HOMEPATH']
println "TMP = " + System.properties['TMP']
println ""
}
task testc(Type:Test) {
// pass a arg to this test like -PMYARG=anything
println ""
println "Parg = " + System.properties['MYARG']
println ""
}
testWorks {
println ""
ENV['ok'] = "good to go"
println "this test is " + ENV['ok']
println ""
}
Run Code Online (Sandbox Code Playgroud)
要使Gradle JVM的系统属性可用于测试,您可以执行以下操作:
test {
systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您已声明其他Test任务:
tasks.withType(Test) {
systemProperties = System.properties
}
Run Code Online (Sandbox Code Playgroud)