2012-02-21 49 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> 
相关问题