3

I have a property

base.number=100

in a property file.

I want to create values 102, 103, 105, etc, depending on the value to be added.

How can I add the numbers to the property and get the added value?

user1969650
  • 35
  • 1
  • 2
  • 5

4 Answers4

7

You need no additional ant tasks or additional scripting languages for math operations, just use the builtin javascript scripting engine java ships with (since jdk 1.6, Sun's own implementation based on rhino 1.6R2) combined with ant api and put in a macrodef for resuse, i.e. :

<project>
  <property name="foo" value="22"/>
  <echo>$${foo} => ${foo}</echo>

  <!-- create macrodef -->
  <macrodef name="math">
   <attribute name="operation"/>
   <attribute name="operator1"/>
   <attribute name="operator2"/>
   <attribute name="result"/>
   <sequential>
    <script language="javascript">
     tmp = 0;
     switch ("@{operation}")
     {
      case "+" :
       tmp = parseInt("@{operator1}") + parseInt("@{operator2}");
       break;
      case "-" :
       tmp = parseInt("@{operator1}") - parseInt("@{operator2}");
       break;
      case "*" :
       tmp = parseInt("@{operator1}") * parseInt("@{operator2}");
       break;
      case "/" :
       tmp = parseInt("@{operator1}") / parseInt("@{operator2}");
       break;
     }
     project.setProperty("@{result}", tmp);
    </script>
   </sequential>
 </macrodef>

  <!-- create new properties -->
  <math operation="/" operator1="${foo}" operator2="11" result="foooo"/>
  <math operation="+" operator1="${foo}" operator2="21" result="fooo"/>
  <!-- overwrite existing property foo -->
  <math operation="+" operator1="${foo}" operator2="1" result="foo"/>
  <echo>
  create    => $${fooo} => ${fooo}
  create    => $${foooo} => ${foooo}
  overwrite => $${foo}  => ${foo}
  </echo> 
</project>

If you need to overwrite an existing userproperty (= those properties defined on commandline via ant -f foobar.xml -Dmyuserproperty=foo ...) you have to use the method :

project.setUserProperty()
Rebse
  • 10,307
  • 2
  • 38
  • 66
  • 1
    This needs to be the answer... thank you! I was using Maven + ant and had to declare the version as 1.7 (defaulted to 1.3). – Smeiff Oct 01 '14 at 14:43
4

You can use the <buildnumber> task, which uses a file name build.number by default. The suggestion in amine's comment link is more general: the <propertyfile> task can set, increment, decrement, or delete properties.

<propertyfile file="number.properties">
    <entry key="base.number" type="int" operation="+" value="1"/>
</propertyfile>
Eric Jablow
  • 7,874
  • 2
  • 22
  • 29
2

Ant contrib has a Math task. It can add numbers among other things. Which means you:

  1. Read the property
  2. Use the math task to add numbers
Jeanne Boyarsky
  • 12,156
  • 2
  • 49
  • 59
2

Here is a macro that uses the javascript scripting engine to allow arbitrary expressions:

<macrodef name="property-exp">
    <attribute name="name" />
    <attribute name="value" />
    <sequential>
        <script language="javascript">
            project.setProperty("@{name}", eval(@{value}));
        </script>
    </sequential>
</macrodef>

<property name="old-version" value="new-version" />
<property-exp name="new-version" value="${old-version} + 1" />
<echo>old=${old-version}, new=${new-version}</echo>
ICR
  • 13,896
  • 4
  • 50
  • 78