2009-07-22 97 views

回答

90

使用类型设置为“dir”的available任务。

例如:

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

的标准方法做条件处理与condition task。在下面的示例中,如果目录存在,则运行doFoo将回显消息,而运行doBar将回显消息,除非该目录存在

dir.check目标由两个doFoo和doBar需要,它集dir.exists属性取决于可用任务的结果真的还是假的。 doFoo目标将只在该propery设置为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提供了额外的任务,包括一个如果任务,可以使处理更简单(和我更直观),您可以下载从download page羚羊任务。

+1

但是,这将设置属性值为true。那我应该如何检查一下情况。我的意思是任何“如果”? – 2009-07-23 05:34:43

+0

为什么验证dir.check在doFoo和doBar之后?不应该是另一种方式? @卖方 – 2017-07-24 17:06:01

29

下面是将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> 

警告:你需要蚂蚁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. 
+2

我喜欢这种解决方案的简单性和表现力。值得额外的繁重工作来安装ant-contrib.jar。 – 2010-12-28 22:05:20

8

这里是我的解决方案,它不需要设置属性,并使用目标与“如果”或“除非”:

宏:

<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}" /> 
1

使用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> 
  • ANT 1.6或早期ANT 1.7不起作用,升级到1.8 ANT释放。
  • 目标属性如果除非评估$ {VAR}语法真/假
  • 条件属性其他值设置为属性(如果可用)的条件是假的,没有它变量没有设置。 NotSet值与显式的假值不同。
  • 调用的任何目标,但如果/除非属性定义其是否实际运行

http://ant.apache.org/manual/properties.html#if+unless
[如果/除非在蚂蚁1.7.1及更早版本,这些属性只能是属性名称。从Ant 1.8.0开始,您可以改为使用属性扩展。与旧款相比,这给你更多的灵活性。

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> 
相关问题