2011-12-06 62 views
4

我有一個基本的螞蟻腳本,我將一組文件複製到任何目標之外的目錄。然後,我想在任何/所有目標都運行後清理這些文件,而不管依賴關係如何。我遇到的主要問題是目標可能是'compile'或'deploywar',所以我不能盲目地從'compile'中調用'cleanUp'目標,因爲'deploywar'可能會被調用。我不能盲目地從'deploywar'調用,因爲它可能不會被調用。我如何定義一個在所有其他必要目標完成(失敗或成功)後會被調用的目標?下面的「cleanUpLib」目標,我想畢竟已經稱爲目標/任何任務都執行:如何強制最終的螞蟻目標執行,無論依賴

<project name="proto" basedir=".." default="deploywar"> 
... 
<copy todir="${web.dir}/WEB-INF/lib"> 
    <fileset dir="${web.dir}/WEB-INF/lib/common"/> 
</copy> 
<target name="compile"> 
    <!-- Uses ${web.dir}/WEB-INF/lib --> 
    .... 
</target> 

<target name="clean" description="Clean output directories"> 
    <!-- Does not use ${web.dir}/WEB-INF/lib --> 
    .... 
</target> 

<target name="deploywar" depends="compile"> 
    <!-- Uses ${web.dir}/WEB-INF/lib --> 
    .... 
</target> 

<target name="cleanUpLib"> 
    <!-- Clean up temporary lib files. --> 
    <delete> 
     <fileset dir="${web.dir}/WEB-INF/lib"> 
      <include name="*.jar"/> 
     </fileset> 
    </delete> 
</target> 

回答

2

構建監聽器解決方案由Rebse指出看起來很有用(+1)。

你可以考慮將「超載」你的目標,這樣的一種替代方案:

<project default="compile"> 

    <target name="compile" depends="-compile, cleanUpLib" 
     description="compile and cleanup"/> 

    <target name="-compile"> 
     <!-- 
      your original compile target 
     --> 
    </target> 

    <target name="deploywar" depends="-deploywar, cleanUpLib" 
     description="deploywar and cleanup"/> 

    <target name="-deploywar"> 
     <!-- 
      your original deploywar target 
     --> 
    </target> 

    <target name="cleanUpLib"> 
    </target> 

</project> 

你真的不能過載,當然一個Ant構建文件,所以目標名稱必須是不同。 (我已經使用了上面的「 - 」前綴,這是一個黑客使得目標是「私人的」 - 也就是說,由於shell腳本arg處理你不能從命令行調用它們,但是當然你仍然可以在Ant中雙擊它們)。

+0

不起作用。如果-compile或-deploywar發生異常,cleanUpLib將不會執行。 – rreyes1979