Search code examples
loopsantant-contrib

Ant: delete using several name paterns in a loop


Out of directory "res" I want to delete all files with names appearing in directories "platforms/${patern.dir}/res", where pattern.dir is a member of comma-separated list specified by parameter pattern.dirs I want to organize that as a loop, so that the number of directories for name patterns can be changed without target modification.

Here is the task that is supposed to do that:

<taskdef resource="net/sf/antcontrib/antlib.xml"/>

<target name="-clean-irrelevant" if="pattern.dirs" >
    <for list="${pattern.dirs}" param="pattern.dir">
         <delete verbose="true">
            <fileset dir="res" includes="**/*" >
                <present present="srconly"
                 targetdir="platforms/${pattern.dir}/res"/>     
            </fileset>  
        </delete>    
    </for>
</target>

When I run the task, I get the message

The following error occurred while executing this line:
for doesn't support the nested "delete" element.

Tried foreach, also antcall of a stand-alone delete task - no luck.

Is there a way to implement the task?


Solution

  • You need to place a sequential element after the for loop, which should contain the tasks to be run for each iteration.

    To access the list parameter pattern.dir, you need to use @{} instead of ${}.

    <target name="-clean-irrelevant" if="pattern.dirs" >
        <for list="${pattern.dirs}" param="pattern.dir">
            <sequential>
                <delete verbose="true">
                    <fileset dir="res" includes="**/*" >
                        <present present="srconly"
                           targetdir="platforms/@{pattern.dir}/res"/>     
                    </fileset>  
                </delete> 
            </sequential>           
        </for>
    </target>