ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bill Rich <b...@wilandra.com>
Subject Re: conditional statements
Date Fri, 15 Feb 2008 00:26:43 GMT
First I must say that Ant is not a process language it is a rules 
language so tasks like <if> and <switch> do not belong in Ant.

Now, is this more like what you want...
<target name="evalx">
    <condition
         property="x.is.true">
         <istrue value="${x}"/>
    </condition>
</target>

<target name="do_if_x_is_true"
    if="x.is.true" depends="evalx">
    <echo>X is True!</echo>
</target>

<target name="do_if_x_is_false"
   unless="x.is.true" depends="evalx">
   <echo>X is False!</echo>
</target>

<target name="the_rest" depends="do_if_x_is_true, do_if_x_is_false">
    <echo>Yadda...</echo>
    <echo>Yadda...</echo>
    <echo>Yadda...</echo>
 </target>

Fully testing this snippet is left as an exercise for the reader. What 
is intended is that your goal is to run the "the_rest" target. It will 
run only if its dependencies are met. Both the "do_if_x_is_true" target 
and the "do_if_x_is_false" target will terminate correctly (run) because 
whether x is true or not their dependencies are met. Those two targets, 
in turn, can not be run unless the "evalx" target is run so they both 
depend on "evalx".

Robin Chaddock wrote:
>
> [quote]
> ...but you must include the ant-contrib-xxx.jar file in your Ant's 
> classpath...
> [/quote]
>
> It's a lot neater, and portable to other peoples configurations if the 
> antcontrib lib jar is simply specified in the classpath attribute of 
> the appropriate taskdef[s].
> e.g.
> <taskdef resource="*net/sf/antcontrib/antcontrib.properties*" 
> classpath="*${ant-contrib.jar}*" />
>
>
> David Weintraub wrote:
>> In standard Ant, you use the <condition> task to set a property, then
>> you can use <antcall> to call a task.
>>
>> <task name="if_else_task">
>>     <condition
>>          property="x.is.true">
>>          <istrue value="${x}"/>
>>     </condition>
>>     <antcall target="do_if_x_is_true"/>
>>     <antcall target="do_if_x_is_false"/>
>>     <echo>Yadda...</echo>
>>     <echo>Yadda...</echo>
>>     <echo>Yadda...</echo>
>> </target>
>>
>> <target name="do_if_x_is_true"
>>     if="x.is.true">
>>     <echo>X is True!</echo>
>> </target>
>>
>> <target name="do_if_x_is_false"
>>    unless="x.is.true">
>>    <echo>X is False!</echo>
>> </target>
>>
>> A little complex, but that's how it's done.
>>
>> AntContrib (as others have pointed out) has an <if> task:
>>
>> <if>
>>     <istrue value="${x}"/>
>>     <then>
>>           <echo>X is True!</echo>
>>     </then>
>>     <else>
>>          <echo>X is False</echo>
>>     </else>
>> </if>
>>
>>
>> This is simpler, but you must include the ant-contrib-xxx.jar file in
>> your Ant's classpath, and include a <taskdef> task in your build.xml
>> file. See the AntContrib website for more information.
>>
>> On Thu, Feb 14, 2008 at 2:42 PM, jonese <gigado@gmail.com> wrote:
>>  
>>> How would i do something like
>>>
>>>  if x = true{
>>>   stuff
>>>  }else{
>>>   other stuff
>>>  }
>>>
>>>  in ant? basically i want to see if a property is true and if it is do
>>>  x instructions, if it's false do y.
>>>
>>>  jonese
>>>
>>>  ---------------------------------------------------------------------
>>>  To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
>>>  For additional commands, e-mail: user-help@ant.apache.org
>>>
>>>
>>>     
>>
>>
>>
>>   
>
>
>
> ________________________________________________________________________
> E-mail is an informal method of communication and may be subject to 
> data corruption, interception and unauthorised amendment for which 
> I-play, a trading name of Digital Bridges Ltd will accept no 
> liability. Therefore, it will normally be inappropriate to rely on 
> information contained on e-mail without obtaining written confirmation.
>
> This e-mail may contain confidential and/or privileged information. If 
> you are not the intended recipient (or have received this e-mail in 
> error) please notify the sender immediately and destroy this e-mail. 
> Any unauthorized copying, disclosure or distribution of the material 
> in this e-mail is strictly forbidden.
>
> (C) 2005. I-play is a trademark and trading name of Digital Bridges 
> Limited. All Rights Reserved.
> ________________________________________________________________________
> This message has been checked for all known viruses by the MessageLabs 
> Virus Scanning Service. For further information visit
> http://www.messagelabs.com/stats.asp

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message