Converting an Ant fileset to multiple apply args

我的梦境 提交于 2019-12-30 03:03:06

问题


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)


回答1:


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}'"/>


来源:https://stackoverflow.com/questions/2148390/converting-an-ant-fileset-to-multiple-apply-args

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!