如何检查使用Ant文件夹是否存在?
我们可以检查文件是否存在,但我们可以做同样的文件夹呢?
如何检查使用Ant文件夹是否存在?
我们可以检查文件是否存在,但我们可以做同样的文件夹呢?
您可以使用可用的任务类型设置为“目录”。
例如:
<available file="${dir}" type="dir"/>
做条件处理的标准方法是用条件的任务 。 在下面的例子中,如果目录存在,而除非目录存在运行doBar会响应消息运行doFoo会响应的消息。
该dir.check目标由两个doFoo和doBar需要,它设置dir.exists属性取决于可用任务的结果真的还是假的。 如果属性格式设置为true的doFoo目标将只运行,如果没有设置或设置为false,它doBar将只运行。
<?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>
羚羊提供了额外的任务,包括一个如果任务,可以使处理更简单(和我更直观),您可以从下载羚羊任务下载页面 。
这里是结合一个小例子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_HOME \ lib目录蚂蚁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>
用法:
<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
[如果/除非在蚂蚁1.7.1及更早版本,这些属性只能是属性名称。 蚂蚁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>