19

I have some files:

dir/foo.txt
dir/bar.txt
dir/foobar.txt

In an Ant apply task, I want to pass the list of files as arguments:

<target name="atask">
    <apply executable="${cmd}" parallel="false" verbose="true">
        <arg value="-in"/>
        <srcfile/>
        <arg value="dir/foo.txt"/>
        <arg value="dir/bar.txt"/>
        <arg value="dir/foobar.txt"/>

        <fileset dir="${list.dir}" includes="*.list"/>
    </apply>
</target>

This works fine, but what if I want to pick the list of files dynamically, using a fileset:

<fileset dir="dir" includes="*.txt"/>

How can I convert this fileset to arg elements - one per file? Something like:

<arg>
    <fileset dir="dir" includes="*.txt"/>
</arg>

instead of

<arg value="dir/foo.txt"/>
<arg value="dir/bar.txt"/>
<arg value="dir/foobar.txt"/>

(This example doesn't work because arg doesn't support fileset)

martin clayton
  • 76,436
  • 32
  • 213
  • 198
Jmini
  • 9,189
  • 2
  • 55
  • 77

1 Answers1

28

Here's an example illustrating the use of the pathconvert task.

The converted path is passed to the executable using <arg line />.

This assumes no spaces in the paths of your *.txt files.

<target name="atask">
    <fileset dir="dir" id="myTxts">
        <include name="*.txt" />
    </fileset>
    <pathconvert property="cmdTxts" refid="myTxts" pathsep=" " />

    <apply executable="${cmd}" parallel="false" verbose="true">
        <arg value="-in" />
        <srcfile />
        <arg line="${cmdTxts}" />

        <fileset dir="${list.dir}" includes="*.list" />
    </apply>
</target>

If you might encounter spaces this should do: as above, but change (hopefully obvious which lines) to:

    <pathconvert property="cmdTxts" refid="myTxts" pathsep="' '" />

and

        <arg line="'${cmdTxts}'"/>
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • I thought It was not possible to mix and but it seems to be OK. This is a nice workaround and it works (I have not tested the case with spaces in the names). – Jmini Jan 28 '10 at 17:47
  • 1
    [Quote:](https://ant.apache.org/manual/using.html#arg) *It is highly recommended to avoid the line version when possible. Ant will try to split the command line in a way similar to what a (Unix) shell would do, but may create something that is very different from what you expect under some circumstances.* – ceving Sep 17 '20 at 07:24