检查文件是否存在于 Ant FileSet 中

Check whether a file exists in an Ant FileSet

我在 Ant 构建文件中使用 FileSet,该文件作为来自外部源的输入被读取。如何检查 FileSet 中是否存在特定文件?

比如正在读取的FileSet是**/src/*.java,我应该可以在里面找到MyClass.java。但是,如果 FileSet 是 **/src/AClass.java, **/src/BClass.java,则 MyClass.java 不在其中。

即使有一种方法可以扩展 FileSet 并进行字符串包含检查,那也应该可行。

有很多方法可以在 FileSet 中创建选择器,但我找不到任何可以说明如何 find/select 给定 FileSet 中的文件的方法。所以我不能用我可以尝试的例子来支持它。任何帮助将不胜感激。

使用 restrict 资源集合仅包含命名文件:

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

我通过将完整的文件集与仅包含我正在查看的这个文件的文件集相交,并验证计数是否等于 1 来实现这一点。其他条件可以很容易地进行 AND-ed 或 OR-编辑

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