Search code examples
filecollectionsantfileset

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.


Solution

  • 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>