【问题标题】:How do I test to see that a directory is empty in ANT?如何测试以查看 ANT 中的目录是否为空?
【发布时间】:2010-06-15 18:48:02
【问题描述】:

如何在ant中测试一个目录是否为空?

【问题讨论】:

  • 我不敢相信没有人问过这个问题……好吧,我猜 ANT 有点老了……
  • ...哦,好吧,它适用于我的自动化。

标签: ant directory


【解决方案1】:

您可以通过setonempty 属性使用pathconvert 任务来执行此操作。

<pathconvert refid="myfileset"
             property="fileset.notempty"
             setonempty="false"/>

仅当 refid 为 myfileset 的文件集不为空时,才会设置属性 fileset.notempty

你只需要用你的目录定义myfileset,没有排除会得到一个目录空测试:

<fileset dir="foo/bar" id="myfileset"/>

有关用例,请参阅 this example

使用 pathconvert 的 setonempty 属性,其值为“false”。 这样,如果文件集为空,则不会设置该属性。这个 很好,因为目标会检查其 if 属性是否为属性 设置与否。

所以你做这样的事情:

<fileset dir="foo/bar" id="myfileset"/>
<target name="fileset.check">
    <pathconvert refid="myfileset" property="fileset.notempty"
setonempty="false"/>
</target>
<target name="main" depends="fileset.check" if="fileset.nonempty">
    <!-- your main work goes here -->
</target>

【讨论】:

    【解决方案2】:

    这只是对tonio's answer 的补充。

    在此示例中,cvs checkout 使用 git 命令模拟:

    • git clonedir 为空时
    • git fetch否则

    <target name="cvs_checkout" depends="git.clone, git.fetch" />
    
    <target name="git.clone" depends="check.dir" unless="dir.contains-files">
      <echo message="Directory ${dir} is empty -} git clone" />
      <exec executable="git">
        <arg value="clone"/>
        <arg value="${repo}"/>
        <arg value="${dir}"/>
      </exec>
    </target>
    
    <target name="git.fetch" depends="check.dir" if="dir.contains-files">
      <echo message="Directory ${dir} contains files -} git fetch" />
      <exec executable="git" dir="${dir}">
        <arg value="fetch"/>
      </exec>
    </target>
    
    <target name="check.dir">
      <fileset dir="${dir}" id="fileset"/>
      <pathconvert refid="fileset" property="dir.contains-files" setonempty="false"/>
    </target>
    

    【讨论】:

      【解决方案3】:

      仅在目录为空时设置属性的脚本解决方案:

      <script language="javascript">
          tests = new java.io.File(project.getProperty("source.test.java.dir"));
          if (tests.list().length == 0) {
              java.lang.System.out.println('no tests: skip.test=true');
              project.setProperty("skip.test", true);
          }
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-14
        • 2014-03-01
        • 2018-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        相关资源
        最近更新 更多