javaantant-contrib

<param> under <foreach> doesn't support nested fileset element


I am trying to perform a task that is supposed to iterate over a set of files. For that I am using the foreach task from ant-contrib-0.3.jar. The problem is that I am trying to pass (as param) to the target not the file path, but the file directory path.

Here is the project:

<project basedir="../../../" name="do-report" default="copy-all-unzipped">
    <xmlproperty keeproot="false" file="implementation/xml/ant/text-odt-properties.xml"/>
    <!--    -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${path.infrastructure}/apache-ant-1.9.6/lib/ant-contrib-0.3.jar"/>
        </classpath>
    </taskdef>
    <!--    -->
    <target name="copy-all-unzipped">
        <foreach target="copy-unzipped">
            <param name="file-path">
                <fileset dir="${path.unzipped}">
                    <include name="**/content.xml"/>
                </fileset>
            </param>
        </foreach>
    </target>
    <!--    -->
    <target name="copy-unzipped">
        <echo>${file-path}</echo>
    </target>
</project>

Running the script I am getting the following message:

BUILD FAILED

C:\Users\rmrd001\git\xslt-framework\implementation\xml\ant\text-odt-build.xml:13: param doesn't support the nested "fileset" element.

I can read in several places on the internet (such as axis.apache.org) that param can have a nested fileset element.


Solution

  • The example of <foreach> you link to is from the Apache Axis 1.x Ant tasks project. This is a different <foreach> than the one from the Ant-Contrib project.

    As manouti says, the Ant-Contrib <foreach> doesn't support nested param elements. Instead, the id of a <fileset> can be passed to the <target> and the <target> can reference the id.

    Or, you can use Ant-Contrib's <for> task (not "<foreach>") which can call a <macrodef>...

    <target name="copy-all-unzipped">
        <for param="file">
            <path>
                <fileset dir="${path.unzipped}">
                    <include name="**/content.xml"/>
                </fileset>
            </path>
            <sequential>
                <copy-unzipped file-path="@{file}"/>
            </sequential>
        </for>
    </target>
    
    <macrodef name="copy-unzipped">
        <attribute name="file-path"/>
        <sequential>
            <echo>@{file-path}</echo>
        </sequential>
    </macrodef>