Jun 23

[Quick Tip] Printing out all matches in an Ant fileset

Tag: ant,java,quicktippmularien @ 10:17 pm

This is one of those things that’s so handy, I can’t believe it hasn’t been posted before. I found a 2006 post from JavaLobby, where R.J. Lorimer writes about how to print out a classpath.

Also useful, but the particular use case I ran into was – one of our build scripts uses a fileset to select incrementally more complex test suites to run. Developers can do a quick check locally with the “short tests” – however, since these are specified as a fileset, it’s hard to know exactly what will run. I wanted to create a simple ant task to take the fileset, and print out everything that matched.

Here’s how I did it. Kudos to some of the posters on the JL article, who were close (Glen Marchesani being very close to what I wanted), but I wanted one result per line, along with some slight tweaks to syntax.

First, you have a typical fileset with an id:

    <fileset dir="${build.classes.dir}" id="unit-tests-fileset">
         <include name="**/*Test*.*"/>
 
         <exclude name="**/ComplexTest.*"/>
    </fileset>

(This is obviously simplified from the build file I was working with, but you get the idea). Next, we create a simple target intended to be invoked from the command line:

    <target  name="inventory-unit-tests">
        <echo-fileset  filesetref="unit-tests-fileset"/>
    </target>

You can see the “filesetref” and “id” parameters match here. The “echo-fileset” is a reference to a relatively simple macro:

    <macrodef name="echo-fileset">
        <attribute name="filesetref" />
        <sequential>
        <for param="file">
            <fileset  refid="@{filesetref}"/>
            <sequential>
                <echo>@{file}</echo>
            </sequential>
        </for>
        </sequential>
    </macrodef>

That’s it. If you run from the command line, you get a list of matching files, one per line, suitable for feeding into grep or other text processing tools:

C:\projects\ant_echo>ant -q -e inventory-unit-tests
C:\projects\ant_echo\classes\test\AUnitTest.class
C:\projects\ant_echo\classes\test\AnotherUnitTest.class

I prefer the -q (quiet) and -e (strip prefixes) options for ant functions like this, but it’s up to you to tweak (post back with any suggestions you have!). Enjoy!

One Response to “[Quick Tip] Printing out all matches in an Ant fileset”

  1. maruzo says:

    Very useful, just little correction, there must be path element:

    <macrodef name="echo-fileset">
            <attribute name="filesetref"/>
            <sequential>
                <for param="file">
                    <path>
                        <fileset refid="@{filesetref}"/>
                    </path>
                    <sequential>
                        <echo>@{file}</echo>
                    </sequential>
                </for>
            </sequential>
        </macrodef>

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>