【问题标题】:Is there is a way to check Ant Target condition before target dependencies get executed有没有办法在执行目标依赖项之前检查 Ant 目标条件
【发布时间】:2012-02-21 11:00:02
【问题描述】:

我试图在我的程序中避免 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”条件,因为我有更多条件可以检查这些目标。

【问题讨论】:

    标签: ant ant-contrib


    【解决方案1】:

    在查看“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>
    

    【讨论】:

    • 是的,我想到了这个选项。试图避免蚂蚁呼叫。所以我想,现在除了使用 antcall 没有更好的选择。感谢您提供解决方案。
    【解决方案2】:

    为避免使用 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>
    

    【讨论】:

      猜你喜欢
      • 2011-11-06
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 2010-09-08
      • 2011-11-30
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多