在我的build.xml中,我想做相当于cmd1 | xargs cmd2(并且还将文件列表存储cmd1到变量中${dependencies}),其中cmd1给出了换行符分隔的路径列表.我无法弄清楚如何在Ant中做到这一点.
<project default="main">
<target name="main">
<exec executable="echo"
outputproperty="dependencies">
<arg value="closure/a.js
closure/b.js
closure/c.js"/>
<redirector>
<outputfilterchain>
<replacestring from="${line.separator}" to=" "/>
<!-- None of these do anything either:
<replacestring from="\n" to=" "/>
<replacestring from="
" to=" "/>
<replaceregex pattern="
" replace=" " flags="m"/>
<replaceregex pattern="\n" replace=" " flags="m"/>
<replaceregex pattern="${line.separator}" replace=" " flags="m"/>
-->
</outputfilterchain>
</redirector>
</exec>
<!-- Later, I need to use each file from ${dependencies} as an argument
to a command. -->
<exec executable="echo">
<!--This should turn into 3 arguments, not 1 with newlines.-->
<arg line="${dependencies}"/>
</exec>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
此过滤器可能适用于第一部分 - 它假设您的所有文件都不以空格字符开头.
<outputfilterchain>
<prefixlines prefix=" " />
<striplinebreaks />
<trim />
</outputfilterchain>
Run Code Online (Sandbox Code Playgroud)
它为每一行添加一个空格前缀,然后删除换行符 - 给出一行,所有文件名由单个空格分隔,但在开头有一个空格.所以trim用来切断它.