Search code examples
antant-contrib

Is there is a way to check Ant Target condition before target dependencies get executed


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.


Solution

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