我有没有办法检查 Ant 中的目录(不是文件)是否存在? [英] Do I have a way to check the existence of a directory in Ant (not a file)?

查看:36
本文介绍了我有没有办法检查 Ant 中的目录(不是文件)是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Ant 检查文件夹是否存在?

How do I check for the existence of a folder using Ant?

我们可以检查文件是否存在,但我们也可以检查文件夹吗?

We can check the existence of a file, but can we do the same for a folder as well?

推荐答案

您使用 可用 类型设置为dir"的任务.

You use the available task with type set to "dir".

例如:

<available file="${dir}" type="dir"/>

进行条件处理的标准方法是使用条件任务.在下面的示例中,如果目录存在,运行 doFoo 将回显一条消息,而运行 doBar 将回显一条消息除非目录存在.

The standard way to do conditional processing is with the condition task. In the example below, running doFoo will echo a message if the directory exists, whereas running doBar will echo a message unless the directory exists.

doFoo 和 doBar 都需要 dir.check 目标,它根据可用任务的结果将 dir.exists 属性设置为 true 或 false.doFoo 目标仅在该属性设置为 true 时运行,而 doBar 仅在未设置或设置为 false 时运行.

The dir.check target is required by both doFoo and doBar, it sets the dir.exists property to true or false depending on the result of the available task. The doFoo target will only run if that propery is set to true and doBar will only run if it is not set or set to false.

<?xml version="1.0"?>
<project name="test" default="doFoo" basedir=".">
  <property name="directory" value="c:\test\directory"/>

  <target name="doFoo" depends="dir.check" if="dir.exists">
    <echo>${directory} exists</echo>
  </target>

  <target name="doBar" depends="dir.check" unless="dir.exists">
    <echo>${directory} missing"</echo>
  </target>

  <target name="dir.check">
    <condition property="dir.exists">
      <available file="${directory}" type="dir"/>
    </condition>
  </target>
</project>

Antelope 提供了额外的任务,包括可以使处理更简单的 If 任务(对我来说,更多的是直观),您可以从 下载页面下载 Antelope 任务.

Antelope provides additional tasks, including an If task that can make the processing simpler (and to me, more intuitive), you can download the Antelope tasks from the download page.

这篇关于我有没有办法检查 Ant 中的目录(不是文件)是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆