Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a command line with Gradle and Save the output result

I want to run a command line with Gradle that this command has an output.
I run this command in windows powershell:
./mybat.bat myArgs when I hit enter, it will print some digit, like this:
123456
I want to run this command with gradle and save this result(123456)
here is some code that I written in android build.gradle file:

task getSomeOutput(type: Exec) {
    workingDir "${buildDir}/output"
    commandLine 'powershell', './mybat.bat' , 'foo'//this is myArgs for example
}

this works and prints the value 123456, but I want to save it in a variable, how can I do that?

like image 699
OneDev Avatar asked Oct 23 '25 02:10

OneDev


1 Answers

the best approach I found is to add '/c' to commandLine arguments and use standardOutput, here some code that might help other people:

task getSomeOutput(type: Exec) {
    workingDir "${buildDir}/output"
    commandLine 'powershell', '/c', './mybat.bat' , 'foo'//this is myArgs for example
    standardOutput = new ByteArrayOutputStream()
    doLast {
        def result = standardOutput.toString()
        println "the result value is: $result"
    }    
}
like image 85
OneDev Avatar answered Oct 25 '25 16:10

OneDev