Search code examples
antant-contribfailonerror

how to ignore a failure ant task?


I have this ant script that is reading from a parameter a list of components and running other ant tasks (build.xml's):

<for list="${components.locations}" param="component" failonany="false">
                <sequential>
                    <property name="@{component}" value="true"/>
                    <if>
                        <and>
                            <available file="${repository.location}/@{component}"/>
                            <available file="${repository.location}/${jars.location}"/>
                        </and>
                        <then>
                            <ant inheritAll="false" antfile="${repository.location}/@{component}/build.xml">
                                <!-- failonerror="false" -->
                                <property name="copy.libs" value="${copy.libs}"/>
                                <property name="repository.location" value="${repository.location}"/>
                                <property name="jars.location" value="${repository.location}/${jars.location}"/>
                            </ant>
                        </then>
                    </if>
                </sequential>
            </for>

The problem is if one component is failing, the script doesn't continue to next one.

I tried running with -k (-keep-going) argument but it doesn't help. I found this property failonerror="false" but it's valid for "exec" tasks and couldn't integrate it with "ant" tasks or inside a "target".

Other direction was "failonany" property of the "for" but I didn't manage setting it up explicitly.

Can you please advice...

Thanks.


Solution

  • First of all, I would suggest deleting ant-contrib.jar and never looking back. Believe me, you will be doing yourself a favor.

    You can use the subant task to iterate Ant builds on a set of directories or files. Simply define a dirset and pass any extra properties you need.

    Instead of using ant-contrib's <if> block, use the standard target's if attribute to switch the entire target on or off. This is much safer and better practice.

    <property name="repository.location" location="repository_location" />
    <property name="jars.location" location="${repository.location}/jars" />
    <property name="components" value="dir1,dir2,dir3" />
    
    <target name="init">
        <condition property="jars.available">
            <available file="${jars.location}" />
        </condition>
    </target>
    
    <target name="default" depends="init" if="jars.available">
        <subant inheritall="false" failonerror="false">
            <dirset id="components.dirs" dir="${repository.location}" includes="${components}" />
            <property name="copy.libs" value="${copy.libs}" />
            <property name="repository.location" value="${repository.location}" />
            <property name="jars.location" value="${jars.location}" />
        </subant>
    </target>