本文介绍了Ant 检查一组文件是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ant, how can i check if a set of files (comma-separated list of paths) exist or not?

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

For example I need to check if all paths listed in myprop exist and if so i want to set a property pathExist:

<property name="myprop" value="path1,path2,path3"/>

因此在示例中,所有 path1 path2 path3 都必须存在才能将 pathExist 设置为 true,否则 false.

So in the example all of path1 path2 path3 must exist to set pathExist to true, otherwise false.

我发现对于单个资源,我可以使用 resourceexist 任务,但我不知道如何使用逗号分隔的路径列表.

I discovered that for a single resource I can use the resourceexist task, but i can't figure out how to use that with a comma-separated list of paths.

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

How can I check the existence for a set of paths? Thanks!

推荐答案

您可以组合使用 文件列表限制condition任务.

You can use a combination of a filelist, restrict and condition task for this.

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

In the below example a filelist is created from the property with the comma-separated list of files. Using restrict a list of the files that don't exist is found. This is placed in a property which will be empty if all the files are found.

<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}" />

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

You could use something like this to generate a failure message listing the missing files:

<fail message="Missing files: ${missing.files}">
    <condition>
        <length string="${missing.files}" when="greater" length="0" />
    </condition>
</fail>

这篇关于Ant 检查一组文件是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 08:58