我正在用Gradle任務運行一些curl代碼,並且我注意到如果我訪問兩次進程的文本,那麼第二次訪問總是失敗。我注意到通過將process.text傳遞給XlmSlurper()。如果我第一次打印process.text,它會在sl crash中崩潰,如果我沒有打印它,那麼唾液就會起作用。 下面是一些說明這種行爲的代碼。首先是任務的版本失敗:爲什麼訪問Groovy進程的getText()兩次會導致失敗?
task hello {
println "****** HELLO ******"
def process = ['curl', '-X', 'POST', '-H', 'Accept: Application/xml', '-H', 'Content-Type: application/json', "http://itemid.sea.redacted.com/item_ids", '-d', '{"user":"Y2VxYnVpbGRhZ2VudDpCdTFsZCE=","request":"guid"}'].execute()
process.waitFor()
println "err.text"
println process.err.text
println "process.text 1 is:"
println process.text
println "process.text 2 is:"
println process.text
}
當此執行輸出:
****** HELLO ******
err.text
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 141 0 85 100 56 2412 1589 --:--:-- --:--:-- --:--:-- 2428
process.text 1 is:
<?xml version="1.0" encoding="utf-8"?><long xmlns="http://redacted.com/">121144</long>
process.text 2 is:
Failed to notify ProjectEvaluationListener.afterEvaluate(), but primary configuration failure takes precedence.
java.lang.IllegalArgumentException: android.compileSdkVersion is missing!
at com.android.build.gradle.internal.SdkHandler.initTarget(SdkHandler.java:80)
at com.android.build.gradle.BasePlugin.ensureTargetSetup(BasePlugin.groovy:462)
但是,如果最後兩個的println行註釋掉然後它是成功的:
task hello {
println "****** HELLO ******"
def process = ['curl', '-X', 'POST', '-H', 'Accept: Application/xml', '-H', 'Content-Type: application/json', "http://itemid.sea.redacted.com/item_ids", '-d', '{"user":"Y2VxYnVpbGRhZ2VudDpCdTFsZCE=","request":"guid"}'].execute()
process.waitFor()
println "err.text"
println process.err.text
println "process.text 1 is:"
println process.text
// println "process.text 2 is:"
// println process.text
}
已經輸出:
****** HELLO ******
err.text
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 141 0 85 100 56 1977 1303 --:--:-- --:--:-- --:--:-- 2023
process.text 1 is:
<?xml version="1.0" encoding="utf-8"?><long xmlns="http://redacted.com/">121145</long>
:help
Welcome to Gradle 2.2.1.
To run a build, run gradlew <task> ...
To see a list of available tasks, run gradlew tasks
To see a list of command-line options, run gradlew --help
BUILD SUCCESSFUL
爲什麼添加/刪除導致這種差異的最後一個printlns?
(也爲什麼會出現?幫助......在第二個版本的輸出)使用./gradlew
在我在命令行中運行這兩種情況下
其同樣的問題如果使用getText()代替文本/