2010-10-31 52 views
5

在我的build.xml中,我想要執行cmd1 | xargs cmd2的等效操作(並將cmd1中的文件列表存儲到變量${dependencies}中),其中cmd1給出了一個換行符分隔的路徑列表。我無法弄清楚如何在Ant中做到這一點。如何在輸出過濾鏈中替換ant中的換行符?

<project default="main"> 
    <target name="main"> 
     <exec executable="echo" 
      outputproperty="dependencies"> 
      <arg value="closure/a.js&#xa;closure/b.js&#xa;closure/c.js"/> 
      <redirector> 
       <outputfilterchain> 
        <replacestring from="${line.separator}" to=" "/> 
        <!-- None of these do anything either: 
        <replacestring from="\n" to=" "/> 
        <replacestring from="&#xa;" to=" "/> 
        <replaceregex pattern="&#xa;" 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> 

回答

6

此過濾器可能會爲第一部分做 - 它假定您的任何文件都不以空格字符開頭。

<outputfilterchain> 
    <prefixlines prefix=" " /> 
    <striplinebreaks /> 
    <trim /> 
</outputfilterchain> 

它的前綴與空間的每一行,然後刪除換行符 - 讓所有的單空格分隔的文件名單行線,但是在開頭的一個空間。所以trim是用來切斷它。

2

感謝馬丁。我還仔細閱讀filterchain documentation後發現了另一種解決方案。

<outputfilterchain> 
    <tokenfilter delimoutput=" "> 
     <!--The following line can be omitted since it is the default.--> 
     <linetokenizer/> 
    </tokenfilter> 
</outputfilterchain>