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

2019-04-18 14:58发布

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

3条回答
Juvenile、少年°
2楼-- · 2019-04-18 15:36

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>
查看更多
三岁会撩人
3楼-- · 2019-04-18 15:41

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>
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-18 15:54

You can use the pathconvert task to do that, with the setonempty property. See this example for an use case.

<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"/>
查看更多
登录 后发表回答