Class IfTask

java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.taskdefs.condition.ConditionBase
net.sf.antcontrib.logic.IfTask
All Implemented Interfaces:
Cloneable

public class IfTask extends org.apache.tools.ant.taskdefs.condition.ConditionBase
Perform some tasks based on whether a given condition holds true or not.

This task is heavily based on the Condition framework that can be found in Ant 1.4 and later, therefore it cannot be used in conjunction with versions of Ant prior to 1.4.

This task doesn't have any attributes, the condition to test is specified by a nested element - see the documentation of your <condition> task (see the online documentation for example) for a complete list of nested elements.

Just like the <condition> task, only a single condition can be specified - you combine them using <and> or <or> conditions.

In addition to the condition, you can specify three different child elements, <elseif>, <then> and <else>. All three subelements are optional. Both <then> and <else> must not be used more than once inside the if task. Both are containers for Ant tasks, just like Ant's <parallel> and <sequential> tasks - in fact they are implemented using the same class as Ant's <sequential> task.

The <elseif> behaves exactly like an <if> except that it cannot contain the <else> element inside of it. You may specify as may of these as you like, and the order they are specified is the order they are evaluated in. If the condition on the <if> is false, then the first <elseif> who's conditional evaluates to true will be executed. The <else> will be executed only if the <if> and all <elseif> conditions are false.

Use the following task to define the <if> task before you use it the first time:


   <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" />
 

Crude Example


 <if>
  <equals arg1="${foo}" arg2="bar" />
  <then>
    <echo message="The value of property foo is bar" />
  </then>
  <else>
    <echo message="The value of property foo is not bar" />
  </else>
 </if>
 

 
 <if>
  <equals arg1="${foo}" arg2="bar" />
  <then>
   <echo message="The value of property foo is 'bar'" />
  </then>

  <elseif>
   <equals arg1="${foo}" arg2="foo" />
   <then>
    <echo message="The value of property foo is 'foo'" />
   </then>
  </elseif>

  <else>
   <echo message="The value of property foo is not 'foo' or 'bar'" />
  </else>
 </if>
 
Author:
Stefan Bodewig
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final class 
     
  • Field Summary

    Fields inherited from class org.apache.tools.ant.ProjectComponent

    description, location, project
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addElse(org.apache.tools.ant.taskdefs.Sequential e)
    A nested <else> element - a container of tasks that will be run if the condition doesn't hold true.
    void
    A nested Else if task
    void
    addThen(org.apache.tools.ant.taskdefs.Sequential t)
    A nested <then> element - a container of tasks that will be run if the condition holds true.
    void
     

    Methods inherited from class org.apache.tools.ant.taskdefs.condition.ConditionBase

    add, addAnd, addAvailable, addChecksum, addContains, addEquals, addFilesMatch, addHttp, addIsFalse, addIsFileSelected, addIsReference, addIsSet, addIsTrue, addNot, addOr, addOs, addSocket, addUptodate, countConditions, getConditions, getTaskName, setTaskName

    Methods inherited from class org.apache.tools.ant.ProjectComponent

    clone, getDescription, getLocation, getProject, log, log, setDescription, setLocation, setProject

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • IfTask

      public IfTask()
  • Method Details

    • addElseIf

      public void addElseIf(IfTask.ElseIf ei)
      A nested Else if task
    • addThen

      public void addThen(org.apache.tools.ant.taskdefs.Sequential t)
      A nested <then> element - a container of tasks that will be run if the condition holds true.

      Not required.

    • addElse

      public void addElse(org.apache.tools.ant.taskdefs.Sequential e)
      A nested <else> element - a container of tasks that will be run if the condition doesn't hold true.

      Not required.

    • execute

      public void execute() throws org.apache.tools.ant.BuildException
      Throws:
      org.apache.tools.ant.BuildException