在ant中使用if..else

有时候我们需要在ant的target中使用一些if...else的逻辑判断,ant给我们提供了两种使用方法:

1.  使用
<if><available file="${path}/classes" type="dir" />
<then>
<echo level="info" message="do sth if dir exist" />
</then>
<else>
<echo level="info" message="do sth if dir does not exist" />
</else>
</if>
使用这种if...else 时要确保ant-contrib.jar 在你的 ANT_HOME\lib directory中。

2. 使用ant condition and available
<project name="test" default="doFoo" basedir=".">
   <property name="directory" value="c:testdirectory"/>
   <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>

For more infroamtion check http://ant.apache.org/manual/Tasks/available.html and http://ant.apache.org/manual/Tasks/condition.html

评论

此博客中的热门博文

Nu förbjuder Kina handel med elfenben

Fader av pingyins

Kineserna vill köpa Volvos kompetens