在Gradle构建脚本中使用系统属性

时间:2013-04-19 17:30:08

标签: properties gradle

这应该是最简单的问题,但我在从Gradle测试任务访问System变量时遇到问题。 what I am doing中必定存在拼写错误,因为我确信这种语法是正确的,但它不起作用。我希望有人可以帮我识别下面这段代码的问题吗?

// 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" )

注意:我也问the question here但是由于它是一个封闭的线程,我不确定我是否会在那里得到答案。另外,我有read the doc彻底,但没有帮助。

我尝试了这些,但也失败了:

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 ""
}

1 个答案:

答案 0 :(得分:9)

要使Gradle JVM的系统属性可用于测试,您可以执行以下操作:

test {
    systemProperties = System.properties
}

或者,如果您已宣布其他Test任务:

tasks.withType(Test) {
    systemProperties = System.properties
}
相关问题