You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by hezjing <he...@gmail.com> on 2010/10/25 04:31:06 UTC

Number expression in Ant?

Hi

Imagine that I have a property with the value "100", then I want to add 50
to it and get the result as 150.

How can I do this in Ant?


-- 

Hez

Re: Number expression in Ant?

Posted by Gilbert Rebhan <gi...@maksimo.de>.
-------- Original Message  --------
Subject: Number expression in Ant?
From: hezjing <he...@gmail.com>
To: ant-users <us...@ant.apache.org>
Date: 25.10.2010 04:31

> Hi
> 
> Imagine that I have a property with the value "100", then I want to add 50
> to it and get the result as 150.
> 
> How can I do this in Ant?

Not with vanilla ant. But there are ant addons like for example
antcontrib (development has fallen asleep since 2006) or more lately
ant-flaka.

<project xmlns:fl="antlib:it.haefelinger.flaka">
  <property name="foobar" value="100" />
  <!--  1. overwrite $foobar -->
  <fl:let>foobar ::= '${foobar}' + 50</fl:let>
  <echo>$${foobar} => ${foobar}</echo>
  <!--  declare new property  -->
  <fl:let>foobaz := '${foobar}' + 50 </fl:let>
  <echo>$${foobaz => ${foobaz}</echo>
</project>


....
[echo] ${foobar} => 100
[echo] ${foobar} + 50 => 150
[echo] ${foobaz => 200
....


For ant-flaka details,download,documentation see =
http://code.google.com/p/flaka/


Regards, Gilbert







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


Re: Number expression in Ant?

Posted by David Weintraub <qa...@gmail.com>.
On Sun, Oct 24, 2010 at 10:31 PM, hezjing <he...@gmail.com> wrote:
> Imagine that I have a property with the value "100", then I want to add 50
> to it and get the result as 150.

Ant isn't a programming language, so it doesn't have a lot of things
you'd think a programming language would have -- such as numeric
processing.

You can take a look at the Antcontrib package
<http://ant-contrib.sourceforge.net/> that many sites use. Antcontrib
contains such tasks that can emulate for loops, if/else statements,
etc. It also has a few property enhancement capabilities like "math".

You can do this in two ways:

<property name="add1" value="100"/>
<property name="add2" value="50"/>

<!-- Method #1 -->
<math result="sum1"
    arg1="${add1}"
    op="+"
    arg2="${add2}">

<!-- Method #2 -->
<math result="sum2"
   <op type="+">
       <num value="${add1}"/>
       <num value="${add2}"/>
   </op>
</math>

As an optional package, you need to do a <taskdef> and add in the
antcontrib.jar file to your libpath.

I highly recommend that you create a special directory in your project
called "antlib", place the antcontrib jarfile there, and then add this
to your build.xml:

<taskdef resource="net/sf/antcontrib/antlib.xml">
  <classpath>
    <fileset dir="${basedir}/antlib"/>
  </classpath>
</taskdef>

This way, people can use your build.xml without having to install the
antcontrib jar file to install antcontrib and configure their ant
installation to run your build. This way, the antcontrib jar is simply
in your project and is a subdirectory from your build.xml file.

-- 
David Weintraub
qazwart@gmail.com

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


Re: Number expression in Ant?

Posted by Henk van Voorthuijsen <vo...@xs4all.nl>.
If the property is in a property file you can use the "PropertyFile" task:

<PropertyFile file="my.properties">
  <entry key="foo" type="int" operation="+" value="50"/>
</PropertyFile>

On Oct 25, 2010, at 4:31 AM, hezjing wrote:

> Hi
> 
> Imagine that I have a property with the value "100", then I want to add 50
> to it and get the result as 150.
> 
> How can I do this in Ant?
> 
> 
> -- 
> 
> Hez


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