<!-- 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 _ HOME lib 目录中的 ANT-Contrib.jar,否则您将无法访问 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.
<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>