1

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

例如,如果正在读取的 FileSet 是**/src/*.java,我应该能够在其中找到 MyClass.java。但是,如果 FileSet 是**/src/AClass.java, **/src/BClass.java,则 MyClass.java 不在其中。

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

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

4

2 回答 2

3

使用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>
于 2015-12-03T20:31:18.360 回答
0

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

<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>
于 2015-12-15T17:20:35.617 回答