1

我试图在我的程序中避免 antcall,因此试图将 antcall 目标移动到目标依赖项。现在我有一个情况:

<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”。
是否有更好的解决方案,其中目标及其依赖项都不会根据条件执行。

我总是可以在 if 条件下进行 antcall。但寻找任何其他选择。
我无法检查 c1、c2、c3 目标中的“skip.c”条件,因为我有更多条件来检查这些目标。

4

2 回答 2

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>
于 2012-02-21T17:14:58.460 回答
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>
于 2015-03-18T16:23:13.943 回答