-->

Is there is a way to check Ant Target condition be

2019-07-26 06:57发布

问题:

I am trying to avoid antcall in my program, so trying to move antcall targets to target dependencies. Now I have a situation:

<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" />

Till now every thing work fine. But if i just want to skip target 'c' and its dependencies from execution,

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

Now the dependencies of target 'c' will be executed and then it checks for condition "skip.c".
Is there a better solution, where both target and its dependencies wont execute based on condition.

I can always go for antcall with if conditions. But looking for any another alternative.
I cant check for "skip.c" conditions in c1,c2,c3 targets as i have more conditions to check in those targets.

回答1:

All dependencies of a target are processed before the "if/unless" test is looked at. There is no built-in method in Ant to avoid this.

Instead, a good approach is to separate the dependencies from the actions. Try the following:

<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>


回答2:

To avoid using the antcall, you would need to put the condition in each of the sub-tasks. Looking at the name "skip.c", it is probably an "unless" condition, like this:

<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>

If you need to do the processing of the conditions at the moment you run task "c", you could do it in a target "check_run_c" like this:

<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>

If there are also instructions in the task "c" that you only want to run conditionally:

<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>