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. 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"/>
回答2:
This is just a complement for tonio's answer.
In this example cvs checkout
is emulated using git
commands:
git clone
whendir
is emptygit 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>