Ant检查是否存在一组文件

ale*_*ars 14 ant

在ant中,我如何检查是否存在一组文件(以逗号分隔的路径列表)?

例如,我需要检查列出的所有路径是否myprop存在,如果是,我想设置属性pathExist:

<property name="myprop" value="path1,path2,path3"/>
Run Code Online (Sandbox Code Playgroud)

因此,在示例中,所有path1 path2 path3必须存在以设置pathExisttrue,否则false.

我发现对于单个资源我可以使用该resourceexist任务,但我无法弄清楚如何使用逗号分隔的路径列表.

如何检查一组路径的存在?谢谢!

mar*_*ton 12

您可以使用的一个组合filelist,restrict并且condition该任务.

在下面的示例中,将使用逗号分隔的文件列表从属性创建文件列表.找到使用restrict不存在的文件列表.这被放置在一个属性中,如果找到所有文件,该属性将为空.

<property name="myprop" value="path1,path2,path3"/>
<filelist id="my.files" dir="." files="${myprop}" />

<restrict id="missing.files">
  <filelist refid="my.files"/>
  <not>
    <exists/>
  </not>
</restrict>

<property name="missing.files" refid="missing.files" />
<condition property="pathExist" value="true" else="false">
    <length string="${missing.files}" length="0" />
</condition>
<echo message="Files all found: ${pathExist}" />
Run Code Online (Sandbox Code Playgroud)

您可以使用类似的方法生成列出丢失文件的失败消息:

<fail message="Missing files: ${missing.files}">
    <condition>
        <length string="${missing.files}" when="greater" length="0" />
    </condition>
</fail>
Run Code Online (Sandbox Code Playgroud)

  • 在Ant 1.7.1中使用此解决方案时,将`xmlns:rsel ="antlib:org.apache.tools.ant.types.resources.selectors"添加到`<project>`元素中.然后将`<not>`更改为`<rsel:not>`,将`<exists>`更改为`<rsel:exists>`. (3认同)
  • 可以使用resourcecount条件缩短解决方案:http://stackoverflow.com/a/19219702/603516 (2认同)