2012-03-08 38 views
2

有沒有一種方法將輸出有條件地重定向到屬性或stdout流,如下面的非工作示例所示?在ant exec任務中有條件地重定向輸出

<macrodef name="mytask"> 
    <attribute name="output" default="STDOUT"/> 
    <sequential> 
     <exec executable="my.exe" outputproperty="@{output}"/> 
    </sequential> 
</macrodef> 

上述示例默認將輸出重定向到屬性STDOUT。相反,我希望它被定向到標準輸出流。

我可以創建mytask_with_stdout作爲上述宏的副本並刪除exec outputproperty,但這會違反DRY原則。

有沒有這樣做的一些不錯的方法?

回答

2

有兩個螞蟻功能,你可以結合起來得到你想要的。

首先,<macrodef>可以通過任何你想要的<element>

其次,可以使用<redirector>來捕獲屬性中<exec>命令的輸出。

我在Windows機器上運行了以下Ant腳本,因此我可以使用cmd.exe的echo命令。用您的my.exe替換cmd.exe:

<project name="exec-redirector-example" default="run"> 
    <macrodef name="mytask"> 
     <attribute name="message"/> 
     <element name="myredirector" optional="true"/> 
     <sequential> 
      <exec executable="cmd.exe"> 
       <arg value="/c"/> 
       <arg value="echo"/> 
       <arg value="@{message}"/> 
       <myredirector/> 
      </exec> 
     </sequential> 
    </macrodef> 

    <target name="run"> 
     <!-- exec outputs to STDOUT by default --> 
     <mytask message="To STDOUT"> 
     </mytask> 

     <!-- exec outputs to a property in this example --> 
     <mytask message="To property"> 
      <myredirector> 
       <redirector outputproperty="my.property"/> 
      </myredirector> 
     </mytask> 

     <echo>${my.property}</echo> 
    </target> 
</project> 
+0

這看起來很有前途。將盡快測試:D謝謝! – Magnus 2012-03-15 10:29:57

+0

經過測試,它的工作原理。 – 2014-10-14 14:44:13

相關問題