Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System properties in a Gradle build script

This should be the simplest of questions but I am having a problem accessing System variable from a Gradle test task. There must be a typo in what I am doing since I am convinced this syntax is correct but it doesn't work. I am hoping someone can help me identify the problem with this code snippet below?

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

NOTE: I also asked the question here but since its a closed thread I am unsure if I will get an answer there. Also, I have read the doc thoroughly but it didn't help.

I tried these and they also fail:

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 ""
}
like image 962
djangofan Avatar asked Jan 14 '23 14:01

djangofan


1 Answers

To make the Gradle JVM's system properties available to tests, you can do:

test {
    systemProperties = System.properties
}

Or, if you have declared additional Test tasks:

tasks.withType(Test) {
    systemProperties = System.properties
}
like image 78
Peter Niederwieser Avatar answered Mar 31 '23 22:03

Peter Niederwieser