我想用 Gradle 運行一個命令列,這個命令有一個輸出。
我在windows powershell中運行這個命令:
./mybat.bat myArgs當我按Enter鍵時,它會列印一些數字,如下所示:
123456
我想用gradle運行這個命令并保存這個結果(123456)
這里是我的一些代碼寫在 android build.gradle 檔案中:
task getSomeOutput(type: Exec) {
workingDir "${buildDir}/output"
commandLine 'powershell', './mybat.bat' , 'foo'//this is myArgs for example
}
這作業并列印值123456,但我想將它保存在一個變數中,我該怎么做?
uj5u.com熱心網友回復:
正如您在官方檔案中看到的那樣
這可以通過以下任務來實作
task executeCMD(type:Exec) {
workingDir '.'
commandLine 'mybat.bat', '>', 'log.txt'
doLast {
println "Executed!"
}
}
這將發送mybat.bat 執行的輸出并將結果設定到一個名為 log 的 txt 檔案中。
這.是您擁有腳本的目錄。
在我的情況下,它是一個專案根目錄。
uj5u.com熱心網友回復:
我發現的最佳方法是將'/c'添加到 commandLine 引數并使用standardOutput,這里有一些可能對其他人有幫助的代碼:
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"
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/411095.html
標籤:
