Cha*_*ara 66 ant scripting build
如何使用Ant检查文件夹是否存在?
我们可以检查文件是否存在,但是我们也可以为文件夹做同样的事情吗?
Ric*_*ler 99
您使用类型设置为"dir" 的可用任务.
例如:
<available file="${dir}" type="dir"/>
Run Code Online (Sandbox Code Playgroud)
进行条件处理的标准方法是使用条件任务.在下面的示例中,如果目录存在,运行doFoo将回显消息,而运行doBar将回显消息,除非该目录存在.
该dir.check目标由两个doFoo和doBar需要,它设置dir.exists属性取决于可用任务的结果真的还是假的.只有当该属性设置为true且doBar仅在未设置或设置为false时才会运行,doFoo目标才会运行.
<?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>
Run Code Online (Sandbox Code Playgroud)
Antelope提供了额外的任务,包括可以使处理更简单的If任务(对我来说更直观),您可以从下载页面下载Antelope任务.
Dan*_*n J 30
这是一个将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>
Run Code Online (Sandbox Code Playgroud)
警告:您需要在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.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案,它不需要设置属性并使用带有'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>
Run Code Online (Sandbox Code Playgroud)
用法:
<assertDirAvailable dir="${dirToCheck}" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78255 次 |
| 最近记录: |