process.text的错误等价物?

时间:2012-05-21 16:07:14

标签: groovy

You can get the entire output stream使用.text:

def process = "ls -l".execute()
println "Found text ${process.text}"

是否有一个简洁的等价物来获取错误流?

2 个答案:

答案 0 :(得分:7)

您可以使用waitForProcessOutput,其中包含两个Appendables(docs here

def process = "ls -l".execute()
def (output, error) = new StringWriter().with { o -> // For the output
  new StringWriter().with { e ->                     // For the error stream
    process.waitForProcessOutput( o, e )
    [ o, e ]*.toString()                             // Return them both
  }
}
// And print them out...
println "OUT: $output"
println "ERR: $error"

答案 1 :(得分:1)

根据tim_yates的回答,我在Jenkins上尝试了这个问题并发现了多个任务的问题:https://issues.jenkins-ci.org/browse/JENKINS-45575

所以这很有效,也很简洁:

def process = "ls -l".execute()
def output = new StringWriter(), error = new StringWriter()
process.waitForProcessOutput(output, error)
println "exit value=${process.exitValue()}"
println "OUT: $output"
println "ERR: $error"