2012-09-13 23 views
1

我有一個混亂的構建。最終,目標執行達15次。大多數目標都被執行了十幾次。這是因爲構建和目標分爲10個獨立的構建文件(build.xmlbuild-base.xmlcompile.xml等)。Ant:在antcall構建文件中設置的屬性會發生什麼?

在許多構建文件中,您在構建文件中的所有目標之外都有<property>任務。這些通常在任何目標被調用之前執行。

這是我build.xml文件:

<import file="build-base.xml"/> 

[...] 

<target name="compile-base"> 
     <antcall target="setup-tmpj"/> 
     <ant antfile="compile.xml" target="compile-base"/> 
     [...] 
</target> 

這裏的compile.xml文件:

<import file="build-base.xml"/> 

<property name="target" value="1.5"/> 
<available file="target/gensrc/com" property=gensrc.exists"/> 

[...] 

<target name="buildAndCompileCodeGen" unless=gensrc.exists"> 
    <blah blah blah/> 
</target> 

<target name="compile-base" depends="buildAndCompileCodeGen"> 
    <blah blah blah/> 
</target> 

我執行此:

$ ant -f build.xml compile-base 

這就要求目標compile-basecompile.xml文件。這取決於compile.xml文件中的目標buildAndCompileCodeGen。但是,只有在未設置屬性gensrc.exists時,纔會執行目標buildAndCompileCodeGen

compile.xml文件是一個<available>的任務,將設置gensrc.exists屬性,但這個任務是位於compile.xml所有目標之外。 <available>任務曾經被調用過,因此gensrc.exist被設置了嗎?

回答

1

好吧,我想通了,這是怎麼回事...

是的,當我通過<ant>任務調用在compile.xml文件compile-base目標,不是目標下的所有任務都執行我調用目標之前執行。這意味着,如果代碼已經存在,則調用buildAndCompileCodeGen目標但不執行。

我所做的是將所有的構建文件合併成一個大文件,並擺脫了所有<ant><antcall>任務。我已將<available>任務放入合併的build.xml文件中。

在原始情況下,我會先做一個clean,然後在compile.xml文件中調用compile-base。那時候,<available>任務就會運行。由於我做了清理,文件不存在,屬性gencode.exists沒有設置,並且buildAndCompileCodeGen目標會運行。

當我組合所有東西時,<available>任務將運行,設置gencode.exists屬性。然後,當我做了一個clean,我會刪除生成代碼。但是,buildAndCompileCodeGen目標仍然不會執行,因爲gencode.exists已被設置。

應該怎樣做是這樣的:

<target name="compile-base" 
    depends="buildAndCompileCodeGen"> 
    <echo>Executing compile-base</echo> 
</target> 

<target name="buildAndCompileCodeGen" 
    depends="test.if.gencode.exists" 
    unless="gencode.exists"> 
    <echo>Executiing buildAndCompileCodeGen</echo> 
</target> 

<target name="test.if.gencode.exists"> 
    <available file="${basedir}/target/gensrc/com" 
     property="gencode.exists"/> 
</target> 

在這種情況下,我打電話compile-base。那會叫buildAndCompileCodeGen。首先將首先撥打test.if.gencode.exists。即使已設置屬性gencode.exists,也會執行此操作。在Ant查看ifunless參數之前,依賴的子句在目標上運行。這樣,我不會設置gencode.exists,直到我準備好執行buildAndCompileCodeGen目標爲止。現在,可用的任務將在之後運行我做了一個清理。

相關問題