如何通过groovy从命令写入日志文件? (gradle这个)

时间:2014-03-18 10:14:11

标签: groovy cmd gradle wsimport

我有一个应用程序,在主gradle文件中我写道:

task myTask {
    def command = "wsimport -s src" + File.separator + "main" + File.separator + "java -d " + classesDir + " path to my WSDL-file -b bindings/bindingFirst.xml -b bindings/bindingSecond.xml>>bindings/OutInfo.log" // Create the String
    def proc = command.execute()                 // Call *execute* on the string
    proc.waitFor()                               // Wait for the command to finish

// Obtain status and output
    println "return code: ${ proc.exitValue()}"
    println "stderr: ${proc.err.text}"
    println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy
}

问题在于:">> bindings / OutInfo.log",为什么?

return code: 1
stderr: Exception in thread "main" com.sun.xml.internal.ws.streaming.XMLReaderException: Unable to create StAX reader or writer
    at com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.create(XMLStreamReaderFactory.java:134)
    at com.sun.tools.internal.ws.wscompile.WsimportOptions.parseBindings(WsimportOptions.java:430)
    at com.sun.tools.internal.ws.wscompile.WsimportTool.run(WsimportTool.java:162)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.tools.internal.ws.Invoker.invoke(Invoker.java:120)
    at com.sun.tools.internal.ws.WsImport.main(WsImport.java:42)
Caused by: java.io.FileNotFoundException: /home/.../exampleWsimport/bindings/bindingSecond.xml>>bindings/OutInfo.log (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at java.net.URL.openStream(URL.java:1037)
    at com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory.create(XMLStreamReaderFactory.java:132)
    ... 8 more

如何在命令wsimpot的日志文件结果中写入? (在gradle(groovy))

1 个答案:

答案 0 :(得分:0)

您不能像这样进行流重定向,可以尝试:

def fs = File.separator

def command = "wsimport -s src${fs}main${fs}java -d $classesDir path to my WSDL-file -b bindings${fs}bindingFirst.xml -b bindings${fs}bindingSecond.xml"

new File( 'bindings/OutInfo.log' ).withWriterAppend { out ->
   out << command.execute().text
}