Searching for files in an Ant script

Steve Neal Development, Tool support Leave a Comment

This is a simple technique for determining whether or not files matching a given pattern exist within your project. Unlike the available task, this does not require that you need to know the exact name of the file when creating the build file. It is therefore very useful for checking existence of generated/downloaded artefacts.

I’ll demonstrate the technique with an Ant target that I wrote to check whether Ivy had downloaded any jars that had the pattern SNAPSHOT in their name:

<target name="ivy-count-snapshot-jars" depends="init" if="ivy.present">
    <resourcecount property="ivy.snapshot.count">
        <fileset dir="${ivylib.dir}" includes="**/*SNAPSHOT*.jar"/>
    <condition property="ivy.snapshot.present">
            <equals arg1="${ivy.snapshot.count}" arg2="0"/>

<target name="ivy-check-snapshot-jars"
     <echo>WARNING: Project uses ${ivy.snapshot.count} SNAPSHOT jar(s).</echo>

The first target does most of the work and is used to count the number of jar files with ‘SNAPSHOT’ in their name; it also sets a property if the count is not zero.

The second target is the main one that should be used to perform the check. It depends on the first target (ensuring that the count is done first) and will only then run if the first target sets the ‘ivy.snapshot.present’ property. Thus the warning message will only get echoed if there are snapshot jars in the project.

Leave a Reply

Your email address will not be published. Required fields are marked *