1
votes

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.

2
Have you considered spelling it correctly?user207421
@EJP yesterday English is not my nature language. I entirely rely on the editor spell check.Hairi

2 Answers

1
votes

See http://ant-contrib.sourceforge.net/tasks/tasks/foreach.html for how to use foreach. The param must be an attribute not a nested element:

<target name="copy-all-unzipped">
    <foreach target="copy-unzipped" param="file-path">
        <fileset dir="${path.unzipped}">
            <include name="**/content.xml"/>
        </fileset>
    </foreach>
</target>
1
votes

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>