【问题标题】:Ant check existence for a set of filesAnt 检查是否存在一组文件
【发布时间】:2011-03-12 09:03:13
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

标签: ant


【解决方案1】:

您可以为此使用filelistrestrictcondition 任务的组合。

在下面的示例中,文件列表是从具有逗号分隔的文件列表的属性创建的。使用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}" />

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

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

【讨论】:

  • 解决方案可以用resourcecount条件缩短:stackoverflow.com/a/19219702/603516
  • 将此解决方案与 Ant 1.7.1 一起使用时,将 xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors" 添加到 &lt;project&gt; 元素。然后将&lt;not&gt; 更改为&lt;rsel:not&gt; 并将&lt;exists&gt; 更改为&lt;rsel:exists&gt;
【解决方案2】:

捆绑条件是检查是否存在多个目录或文件的最短解决方案:

<condition property="pathExist">
 <and>
  <available file="/foo/bar" type="dir"/>
  <available file="/foo/baz" type="dir"/>
  <available file="path/to/foobar.txt"/>
  ...
 </and>
</condition>

要检查以逗号分隔的路径列表,请使用 Ant 插件 Flaka ,f.e. :

<project xmlns:fl="antlib:it.haefelinger.flaka">

 <!-- when you have a cvs property use split function
      to get your list to iterate over -->
 <property name="checkpath" value="/foo/bar,/foo/baz,/foo/bazz"/>
  <fl:for var="file" in="split('${checkpath}', ',')">
    <fl:fail message="#{file} does not exist !!" test="!file.tofile.exists"/>
  </fl:for>               
</project>

另一种可能性是使用带有 jvm 脚本语言(如 groovy、beanshell .. 等)的 scriptcondition 任务。

【讨论】:

    【解决方案3】:

    这可以通过 Ant 的resource collections 的集合操作来解决。如果您计算所需文件列表与现有文件列表的交集,则它不能与所需文件列表不同。这显示了如何做到这一点:

    <property name="files" value="a.jar,b.jar,c.jar"/>
    
    <target name="missing">
      <condition property="missing">
        <resourcecount when="ne" count="0">
          <difference id="missing">
            <intersect>
              <filelist id="required" dir="." files="${files}"/>
              <fileset id="existing" dir="." includes="*.jar"/>
            </intersect>
            <filelist refid="required"/>
          </difference>
        </resourcecount>
      </condition>
    </target>
    
    <target name="check" depends="missing" if="missing">
      <echo message="missing: ${toString:missing}"/>
    </target>
    

    check 目标仅在文件丢失时报告丢失文件列表。

    【讨论】:

      猜你喜欢
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      相关资源
      最近更新 更多