如何测试 ANT 中的目录是否为空?

发布于 2024-09-05 09:30:27 字数 25 浏览 2 评论 0原文

如何测试 ant 中的目录是否为空?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-09-12 09:30:27

您可以使用 pathconvert 任务来执行此操作,带有 setonempty 属性。

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

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

您只需使用目录定义 myfileset ,并且没有排除会获得目录空测试:

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

请参阅 此示例用于用例:

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

所以你做类似的事情:

>
<目标名称=“fileset.check”>
    

<目标名称=“main”取决于=“fileset.check”if=“fileset.nonempty”>
    

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>
谁把谁当真 2024-09-12 09:30:27

这只是对tonio的答案的补充。

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

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

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>
假装不在乎 2024-09-12 09:30:27

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文