How do I test to see that a directory is empty in ANT?

萝らか妹 提交于 2019-12-10 00:53:27

问题


How can one test to see that a directory is empty in ant?


回答1:


You can use the pathconvert task to do that, with the setonempty property.

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

will set the property fileset.notempty only if the fileset those refid is myfileset is not empty.

You just have to define myfileset with your directory, and no excludes do get a directory empty test:

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

See this example for a use case:

use the setonempty attribute of pathconvert, with the value "false". This way, if the fileset is empty, the property will not be set. this is good since targets check with their if attribut whether a property is set or not.

so you do something like :

<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:


This is just a complement for tonio's answer.

In this example cvs checkout is emulated using git commands:

  • git clone when dir is empty
  • git fetch else

<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:


scripted solution that sets a property only when the directory is empty:

<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>


来源:https://stackoverflow.com/questions/3048100/how-do-i-test-to-see-that-a-directory-is-empty-in-ant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!