techjourneyman
techjourneyman

Reputation: 1813

Check whether a file exists in an Ant FileSet

I am using FileSet in an Ant build file that is being read as an input from external source. How can I check if a specific file exists in the FileSet?

For example, if the FileSet being read is **/src/*.java, I should be able to find MyClass.java in it. However, if the FileSet is **/src/AClass.java, **/src/BClass.java, then MyClass.java is not in it.

Even if there is a way I can expand the FileSet and do a string contains check, then that should work.

There are plenty of ways to create selectors within a FileSet, but there is nothing I could find that tells how to find/select a File in a given FileSet. So I can't back it up with an example that I could try. Any help will be very appreciated.

Upvotes: 1

Views: 1313

Answers (2)

techjourneyman
techjourneyman

Reputation: 1813

I implemented that by doing an intersection of the complete Fileset with the Fileset containing just this one file that I am looking, and verifying that if the count equals 1. The other conditions can be easily AND-ed or OR-ed.

<target name="checkSomething">
        <condition property="something.present">
            <and>
                <available file="/src/theFile"/>
                <resourcecount when="equal" count="1">                  
                    <intersect>
                        <fileset dir="${src.dir}" includes="*"/>
                        <fileset dir="${src.dir}" includes="**/src/something.java"/>
                    </intersect>
                </resourcecount>
            </and>
        </condition>
        <echo message="Something present: ${something.present}"/>
    </target>

Upvotes: 0

M A
M A

Reputation: 72884

Using the restrict resource collection to include only the named file:

<project ... xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors">

...

<restrict id="filtered.fileset">
    <fileset refid="source.fileset"/>
    <rsel:name name="MyClass.java"/>
</restrict>

<condition property="file.found" value="yes">
    <resourcecount when="greater" count="0" refid="filtered.fileset" />
</condition>

Upvotes: 2

Related Questions