2013-05-31 27 views
0

我有一個共享的螞蟻腳本b.ant,它在內部使用antcall。它計算客戶端腳本使用的屬性。我使用include而不是import客戶端腳本來避免無意中覆蓋目標,但這給我帶來了一個antcall問題。在包含的ant文件中使用「antcall」

當使用include時,b中的所有目標都是前綴,b中的depends屬性會相應更新。但是,對於antcall,這不是正確的。 有沒有辦法處理這個問題,即讓antcall總是調用「本地」螞蟻目標?

我可以通過使用import解決這個問題,但我會得到所有覆蓋問題。不可能使用depends而不是antcall。


示例文件

我有兩個文件:

a.ant

<project> 
    <include file="b.ant" as="b" /> 
    <target name="test-depends" depends="b.depend"> 
     <echo>${calculated-property}</echo> 
    </target> 
    <target name="test-call" depends="b.call"> 
     <echo>${calculated-property}</echo> 
    </target> 
    </project> 

b.ant

<project> 
    <target name="depend" depends="some-target"> 
    <property name="calculated-property" value="Hello World"/> 
    </target> 
    <target name="call"> 
    <antcall target="some-target" inheritrefs="true"/> 
    <property name="calculated-property" value="Hello World"/> 
    </target> 
    <target name="some-target"/> 
</project> 

實施例輸出

調用test-depend作品如預期,但test-call失敗,此輸出:

b.call: 

BUILD FAILED 
D:\ws\rambo2\ws-dobshl\ant-test\b.ant:6: The following error occurred while executing this line: 
Target "some-target" does not exist in the project "null". 

Total time: 258 milliseconds 

回答

2

Ant是一個依賴矩陣規範語言。通常一堆<antcall/>,<ant/>,<include/><import/>是一個寫得不好的構建腳本的跡象。這是一個試圖強制Ant像編程語言一樣行事的開發者。

對於開發者來說,將程序拆分成更小的文件是有意義的。即使Python和Perl腳本也可以從中受益。但是,分解Ant構建腳本通常會導致問題。我們有一位開發人員經歷了每個項目,並將所有build.xml文件分解爲六個或七個獨立的構建文件,以便提高的過程。它基本上打破了整個Ant依賴機制。爲了解決這個問題,他隨後拋出了一堆<ant/>調用和<include>任務。最後,這意味着每個目標被稱爲12到20次。

不使用<import/><antcall/>不是一條硬性規定。但是,我一直在使用Ant多年,很少使用這些機制。當我這樣做時,通常需要共享一個共享的構建文件,即多個項目將使用(這聽起來像是你所擁有的),但不是在我的共享構建文件中定義目標,而是定義宏。這消除了您正在使用的目標命名空間問題,並且宏更好地工作,因爲它們更像Ant任務。在Ant 1.8中引入<local/>尤其如此。

看看您是否可以使用<macrodef/>而不是目標重構共享構建文件。這將使包含共享構建文件變得更加容易。

0

給一個name<project>在b.ant然後更改<antcall>target

<project name="b"> <!-- Give the project a name --> 
    <target name="depend" depends="some-target"> 
    <property name="calculated-property" value="In b.depend"/> 
    </target> 
    <target name="call"> 
    <!-- Specify the name of the project containing the target --> 
    <antcall target="b.some-target" inheritrefs="true"/> 
    <property name="calculated-property" value="In b.call"/> 
    </target> 
    <target name="some-target"/> 
</project> 

ant -f a.ant test-call結果:

b.call: 

b.some-target: 

test-call: 
    [echo] In b.call 

BUILD SUCCESSFUL 

所改動b.ant, a.ant中的<include>可通過刪除as屬性進行簡化:

​​
相關問題