ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Holst Swende <>
Subject New iterating task
Date Thu, 18 Dec 2008 09:16:21 GMT
Hi all,

I have a little utility-task that I wrote because I did not find any 
better alternative - but maybe I missed something obvious. So I thought 
I would contribute it, if there is any interest.

Anyway, it is a kind of file iterator meant to be used on tasks that are 
not written to handle multiple files. Also a few other uses, such as 
manipulating and/or preserve paths and filenames in the process . Here 
is how I use it :

    <taskdef resource="antutilstasks" classpath="jar/antutils.jar" />

    <target name="testiterate">
        <iterate target="myecho">
            <fileset dir="." includes="**\*.java" />
            <param name="test" value="Works fine" />

    <target name="myecho">
                Dir : ${dir}
                Path  : ${path}
                Extra-param: ${test}

What it does is, for each entry in the fileset, it calls the target 
(myecho), and sets these properties : dir, path, filename, extension and 
any supplied extra parameters. It just lists files, so far.

The fully qualified path is created in this manner :

dir + path + filename + "." + extension

One way of using it is if I have a dir-structure, say a photoalbum, 
where thumbnails have already been generated in the same structure. If I 
want to copy all thumbnails, but keep their directory structure, normal 
file-copy will not suffice (since it will copy all to same directory). 
But I can do this :

    <target name="copythumbs">
        <iterate target="mycopy">
            <fileset dir="photos" includes="**\*_thumb.jpeg" />
    <target name="mycopy">   
        <copy file="${dir}${path}${filename}.${extension}" 

The todir will retain path, and copy files into e.g 
"thumbs/2008/01/21/img01_thumb.jpg" .

And if I have a task that scales images, I could set it up like this :

    <target name="createthumbs">
        <iterate target="mythumbgenerator">
            <fileset dir="photos" includes="**\*.jpeg" />
            <param name="size" value="100x100" />
  <target name="mythumbgenerator">   
        <rescale file="${dir}${path}${filename}.${extension}" 
outfile="thumbs/${path}${filename}_thumb.${extension}" size="${size}"/>

This way, I can iterate a task not built for it (in this case "rescale") 
and be pretty flexible with the input parameters to that task.

So, have I re-invented the wheel?

Martin Holst Swende

Martin Holst Swende ................. MSC Konsult AB
tel: +46(0)70 9519098 ............... Vasagatan 52 .......... 111 20 Stockholm

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message