Search code examples
javaantant-contrib

Problems with iterating through fileset


I try to iterate over a directory of wsdl's. For the first I would be happy just to get an output of each file.

<target name="messages">
    <foreach target="wsdlList" param="wsdlfile">
        <path>
            <fileset dir="${base.wsdl.src}">
                <include name="*.wsdl" />
            </fileset>
        </path>
    </foreach>
</target>

<target name="wsdlList">
    <echo message="${wsdlfile}" />
</target>

The output I get is wsdlList: [echo] ${wsdlfile} instead of all wsdl files I expected.


Solution

  • Instead of using <foreach>, use the newer <for> task. You need to point to antlib.xml and not antcontrib.properties in <taskdef>:

    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <fileset dir="${ivy.dir}/antcontrib">
                <include name="ant-contrib*.jar"/>
            </fileset>
        </classpath>
    </taskdef>
    

    Now, you can do this:

    <target name="messages">
        <for param="wsdl.file">
            <fileset dir="${base.wsdl.src}">
                <include name="*.wsdl" />
            </fileset>
            <sequential>
                <echo message="@{wsdl.file}" />  <!-- Note "@" and not "$" -->
            </sequential>
        </for>
    </target>
    

    Note that this is @{wsdl.file} and not ${wsdl.file}. That's a parameter that can have a different value each time.