user1969650
user1969650

Reputation: 45

How to add any number to a property from a property file

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?

Upvotes: 3

Views: 7413

Answers (4)

ICR
ICR

Reputation: 14162

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>

Upvotes: 2

Rebse
Rebse

Reputation: 10377

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()

Upvotes: 7

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

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

Upvotes: 2

Eric Jablow
Eric Jablow

Reputation: 7899

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>

Upvotes: 4

Related Questions