Ant exec
任务具有一个输出属性,该属性可用于告诉Ant输出到哪里。我用它来将输出重定向到文件。关键是,如果我对输出不做任何事情,那么Ant所打印的内容并不能提供太多帮助-它并不完整。
是否有将输出属性设置为System.out
的方法?
最佳答案
在Windows上使用ant的apply
或exec
任务执行批处理文件时,我发现在某些特殊情况下,某些stdout和stderr无法被ant捕获。 (例如:如果您调用了一个批处理文件,而该批处理文件又调用了其他命令(例如node.exe
),则子node.exe
进程中的stdout和stderror将丢失。)
我花了很长时间尝试调试它!似乎已捕获了批处理文件的stdout和stderr,但是 Ant 看不到批处理文件调用的命令。 (也许是因为它们是单独的子进程)。如上所述,使用output
和error
属性没有帮助,因为仅捕获了部分stdout和/或stderr。
我想出的解决方案(技巧)是在命令末尾添加以下参数:
<!--Next arg: forces node's stderror and stdout to a temporary file-->
<arg line=" > _tempfile.out 2<&1"/>
<!--Next arg: If command exits with an error, then output the temporary file to stdout, -->
<!--delete the temporary file and finally exit with error level 1 so that -->
<!--the apply task can catch the error if @failonerror="true" -->
<arg line=" || (type _tempfile.out & del _tempfile.out & exit /b 1)"/>
<!--Next arg: Otherwise, just type the temporary file and delete it-->
<arg line=" & type _tempfile.out & del _tempfile.out &"/>
由于此hack仅适用于Windows,因此请记住将
@osfamily="windows"
添加到apply
或exec
任务。并为`@ osfamily =“unix”等创建类似的任务,但是没有这些额外的参数。