---
title: "ANT antcontrib使用"
author: "aa00aa00"
date: "2011-09-26T14:57:04Z"
tags: []
categories: ["开源软件"]
slug: "ant-antcontrib"
draft: false
lastmod: "2011-09-26T15:50:25Z"
id: 1180669
url: "https://aa00aa00.iteye.com/blog/1180669"
---

**写在前面:最近用ANT脚本用的比较的多,发现在ANT中使用条件判断有些麻烦,就查点资料,:(1)可以使用condition进行逻辑的判断,接受的属性是property,相当于boolean xx=true(或false),if,unless相当于if-else.(2)可以使用antcontrib.jar这个包文件,大概看了一下,就是将condition拿掉,使用我们熟悉的if-else-then的结果进行业务逻辑的判断!!**

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;project name="cvs package" basedir="." default="packing\_cvs\_package.init"&gt;  
   
  &lt;!--&lt;property name="hello" value="true"/&gt;--&gt;  
 &lt;!--ant-contrib classpath--&gt;  
  &lt;input message="Please input the hello name:" addproperty="hello" /&gt;  
   
  &lt;taskdef resource="net/sf/antcontrib/antcontrib.properties"&gt;  
        &lt;classpath&gt;  
            &lt;pathelement location="E:/apache-ant-1.8.2/ant-contrib/ant-contrib-1.0b2.jar"/&gt;  
        &lt;/classpath&gt;  
  &lt;/taskdef&gt;

  &lt;!--default--&gt;  
 &lt;target name="packing\_cvs\_package.init"&gt;  
 &lt;if&gt;  
  &lt;equals arg1="${hello}" arg2="true"/&gt;  
  &lt;then&gt;  
     &lt;echo &gt;${hello} value is true&lt;/echo&gt;  
  &lt;/then&gt;  
 &lt;elseif&gt;  
  &lt;equals arg1="${hello}" arg2="false"/&gt;  
  &lt;then&gt;  
   &lt;echo &gt;${hello} value is false&lt;/echo&gt;  
  &lt;/then&gt;  
 &lt;/elseif&gt;  
  &lt;else&gt;  
    &lt;echo &gt;${hello} value is unknowe&lt;/echo&gt;  
  &lt;/else&gt;  
 &lt;/if&gt;  
 &lt;/target&gt;

&lt;/project&gt;

 

**上面的有些路径写绝对路径了,修改下:**

 

&lt;project name="imported" basedir="." default="packing\_cvs\_package.init"&gt;

  &lt;dirname property="imported.basedir" file="${ant.file.imported}"/&gt;   
 &lt;property file="${imported.basedir}/base.properties"/&gt;  
  &lt;input message="Please input the hello name:" addproperty="hello" /&gt;  
   
  &lt;!--ant-contrib classpath--&gt;  
  &lt;taskdef resource="net/sf/antcontrib/antcontrib.properties"&gt;  
        &lt;classpath&gt;  
            &lt;pathelement location="${path}"/&gt;  
        &lt;/classpath&gt;  
  &lt;/taskdef&gt;  
  &lt;!--default--&gt;  
 &lt;target name="packing\_cvs\_package.init"&gt;  
 &lt;if&gt;  
  &lt;equals arg1="${hello}" arg2="true"/&gt;  
  &lt;then&gt;  
     &lt;echo &gt;${hello} value is true&lt;/echo&gt;  
  &lt;/then&gt;  
 &lt;elseif&gt;  
  &lt;equals arg1="${hello}" arg2="false"/&gt;  
  &lt;then&gt;  
   &lt;echo &gt;${hello} value is false&lt;/echo&gt;  
  &lt;/then&gt;  
 &lt;/elseif&gt;  
  &lt;else&gt;  
    &lt;echo &gt;${hello} value is unknowe&lt;/echo&gt;  
  &lt;/else&gt;  
 &lt;/if&gt;  
 &lt;/target&gt;  
   
&lt;/project&gt;

 

base.properties文件的内容:

path=E:/apache-ant-1.8.2/ant-contrib/ant-contrib-1.0b2.jar

就是一个地址!!