Dean Povey
Dean Povey

Reputation: 9446

Ant script to find a file in a path and add the full path to a property

I have a need to find a file in a path from ant. Essentially I have a standard path style variable like DIR1:DIR2 and I need to locate a file called foo in that path and put the full path of it in a variable.

Is that possible to do with Ant?

Upvotes: 1

Views: 1587

Answers (1)

Mark O'Connor
Mark O'Connor

Reputation: 77951

Use the pathconvert task with a mapper element.

For example:

<project name="demo" default="run">

    <path id="files.path">
        <fileset dir="dir1"/>
        <fileset dir="dir2"/>
    </path>

    <target name="run">
        <pathconvert property="path.output" refid="files.path">
            <globmapper from="*foo.jar" to="*foo.jar"/>
        </pathconvert>

        <echo message="Output: ${path.output}"/>
    </target>

</project>

Test

ANT project with the following files:

$ tree
.
|-- build.xml
|-- dir1
|   `-- subdir1
|       `-- foo.jar
`-- dir2
    `-- nogood.jar

3 directories, 3 files

Running example

$ ant
Buildfile: /home/me/build.xml

run:
     [echo] Output: /home/me/dir1/subdir1/foo.jar

BUILD SUCCESSFUL

Upvotes: 3

Related Questions