2012-02-21 124 views
1

我想避免我的程序中有antcall,所以試圖將antcall目標移動到目標依賴項。現在我有一種情況:有沒有辦法在執行目標依賴之前檢查Ant Target條件

<target name="test" depends="a,b,c" /> 

<target name="a" depends="a1,a2,a3" /> 
<target name="b" depends="b1,b2,b3" /> 
<target name="c" depends="c1,c2,c3" /> 

直到現在,每件事情都很好。但是,如果我只是想跳過目標「C」,並從執行它的依賴,

<target name="c" depends="c1,c2,c3" if="skip.c" /> (considering the property "skip.c" is already set) 

現在的目標「C」的依賴將被執行,然後它會檢查條件「skip.c」。
是否有更好的解決方案,其中目標及其依賴關係不會根據條件執行。

如果條件允許的話,我總是可以去找antcall。但尋找任何其他的選擇。
我無法檢查c1,c2,c3目標中的「skip.c」條件,因爲我有更多的條件來檢查這些目標。

回答

2

在查看「if/unless」測試之前處理目標的所有依賴關係。 Ant中沒有內置的方法來避免這種情況。

相反,一個好方法是將依賴關係與操作分開。請嘗試以下操作:

<target name="test" depends="a,b,c" /> 

<target name="a" depends="a1,a2,a3" /> 
<target name="b" depends="b1,b2,b3" /> 
<target name="c"> 
    <condition property="skip.c.and.dependents"> 
     <or> 
      <isset property="prop1"/> 
      <isset property="prop2"/> 
     </or> 
    </condition> 

    <antcall target="do-c-conditionally"/> 
</target> 

<target name="do-c-conditionally" unless="skip.c.and.dependents"> 
    <antcall target="do-c"/> 
</target> 

<target name="do-c" depends="c1,c2,c3"> 
    <!-- former contents of target c --> 
</target> 
+0

是的,我想的這個選項。試圖避免antcall。所以我想,現在沒有比使用antcall更好的選擇了。感謝您提供解決方案。 – raviinter 2012-02-22 08:15:21

0

要避免使用antcall,您需要將條件放在每個子任務中。在名爲「skip.c」 來看,它可能是一個「除非」條件,就像這樣:

<target name="test" depends="a,b,c" /> 

<target name="a" depends="a1,a2,a3" /> 
<target name="b" depends="b1,b2,b3" /> 
<target name="c" depends="c1,c2,c3" /> 

<target name="c1" unless="skip.c"> 
     <!-- contents of target c1 --> 
</target> 
<target name="c2" unless="skip.c"> 
     <!-- contents of target c2 --> 
</target> 
<target name="c3" unless="skip.c"> 
     <!-- contents of target c3 --> 
</target> 

如果你需要做的條件,處理在運行任務「C」的那一刻,你可以做一個目標「check_run_c」是這樣的:

<target name="test" depends="a,b,c" /> 

<target name="a" depends="a1,a2,a3" /> 
<target name="b" depends="b1,b2,b3" /> 
<target name="c" depends="check_run_c,c1,c2,c3" /> 
<target name="check_run_c"> 
    <condition property="run.c"> 
     <!-- set this property "run.c" if the "c*" targets should run... --> 
     <or> 
      <isset property="prop1"/> 
      <isset property="prop2"/> 
     </or> 
    </condition> 
</target> 
<target name="c1" if="run.c"> 
     <!-- contents of target c1 --> 
</target> 
<target name="c2" if="run.c"> 
     <!-- contents of target c2 --> 
</target> 
<target name="c3" if="run.c"> 
     <!-- contents of target c3 --> 
</target> 

如果也有在任務「C」的指示,你只需要有條件地運行:

<target name="test" depends="a,b,c" /> 

<target name="a" depends="a1,a2,a3" /> 
<target name="b" depends="b1,b2,b3" /> 
<target name="c" depends="check_run_c,c1,c2,c3" if="run.c" > 
     <!-- contents of target c --> 
</target> 
<target name="check_run_c"> 
    <condition property="run.c"> 
     <!-- set this property "run.c" if the "c*" targets should run... --> 
     <or> 
      <isset property="prop1"/> 
      <isset property="prop2"/> 
     </or> 
    </condition> 
</target> 
<target name="c1" if="run.c"> 
     <!-- contents of target c1 --> 
</target> 
<target name="c2" if="run.c"> 
     <!-- contents of target c2 --> 
</target> 
<target name="c3" if="run.c"> 
     <!-- contents of target c3 --> 
</target> 
相關問題