How to add any number to a property from a propert

2019-05-21 13:43发布

问题:

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?

回答1:

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


回答2:

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


回答3:

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>


回答4:

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>


标签: ant