【发布时间】:2010-11-12 22:26:38
【问题描述】:
如何使用 Ant 检查文件夹是否存在?
我们可以检查文件是否存在,但我们可以对文件夹做同样的事情吗?
【问题讨论】:
如何使用 Ant 检查文件夹是否存在?
我们可以检查文件是否存在,但我们可以对文件夹做同样的事情吗?
【问题讨论】:
您使用类型设置为“dir”的available 任务。
例如:
<available file="${dir}" type="dir"/>
进行条件处理的标准方法是使用condition task。在下面的示例中,如果目录存在,则运行 doFoo 将回显一条消息,而运行 doBar 将回显一条消息除非该目录存在。
doFoo 和 doBar 都需要 dir.check 目标,它根据可用任务的结果将 dir.exists 属性设置为 true 或 false。 doFoo 目标仅在该属性设置为 true 时运行,doBar 仅在未设置或设置为 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 任务,可以使处理更简单(对我来说,更直观),您可以从 download page 下载 Antelope 任务。
【讨论】:
depends 属性。我认为将所谓的“公共”目标放在顶部,将“私人”实用程序目标放在底部会更干净
这是一个将available 元素合并到if 测试中的小示例。
<!-- Test if a directory called "my_directory" is present -->
<if>
<available file="my_directory" type="dir" />
<then>
<echo message="Directory exists" />
</then>
<else>
<echo message="Directory does not exist" />
</else>
</if>
警告:您需要 ant-contrib.jar 在您的 ANT_HOME\lib 目录中,否则您将无法访问 if 元素,并且您的脚本将因以下错误而失败:
Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
【讨论】:
这是我的解决方案,它不需要设置属性和使用带有“if”或“unless”的目标:
宏:
<macrodef name="assertDirAvailable">
<attribute name="dir" />
<sequential>
<fail message="The directory '@{dir}' was expected to be available but is not">
<condition>
<not>
<available file="@{dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</macrodef>
用法:
<assertDirAvailable dir="${dirToCheck}" />
【讨论】:
我的解决方案使用 ANT 1.8 版本,如果/除非不支持 ${evalTrueOrFalse} 语法,旧版本可能无法工作。
<?xml version="1.0" encoding="UTF-8"?>
<project name="DoMagic" default="build" basedir=".">
<property environment="env" />
<property name="name" value="Do the ANT Magic" />
<property name="somedir" value="./must_exist_folder"/>
<tstamp><format property="TODAY" pattern="yyyy-MM-dd HH:mm:ss" /></tstamp>
<target name="doMagic" if="${dir.exists}">
<echo message="Do the magic stuff" />
</target>
<target name="doUsage" unless="${dir.exists}">
<echo message="Do usage and help" />
</target>
<target name="build">
<echo message="Do the magic" />
<condition property="dir.exists" else="false"><available file="${somedir}" type="dir" /></condition>
<echo message="Folder found: ${dir.exists}" />
<antcall target="doCustomize"></antcall>
<antcall target="doUsage"></antcall>
</target>
</project>
http://ant.apache.org/manual/properties.html#if+unless
[If/Unless] 在 Ant 1.7.1 和更早版本中,这些属性只能是属性名称。从 Ant 1.8.0 开始,您可以改为使用属性扩展。与旧样式相比,这为您提供了额外的灵活性。
【讨论】:
这是另一种方法,允许在不使用 ant-contrib.jar 的情况下只调用一个任务。
<target name="my-task" depends="dir-check">
<antcall target="my-task-install"/>
<antcall target="my-task-update"/>
</target>
<target name="my-task-install" unless="dir.exists" >
{some task}
</target>
<target name="my-task-update" if="dir.exists" >
{another task}
</target>
<target name="dir-check">
<condition property="dir.exists">
<available file="my-dir" type="dir" />
</condition>
</target>
【讨论】:
这是另一个涉及for 循环的示例。如果目录不存在则失败。
<for list="dir1/, dir2/, dir3/" param="local.dir" >
<sequential>
<fail message="Directory @{local.dir} does not exist">
<condition>
<not>
<available file="@{local.dir}" type="dir" />
</not>
</condition>
</fail>
</sequential>
</for>
【讨论】: