How to add any number to a property from a propert

2019-05-21 13:25发布

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?

标签: ant
4条回答
对你真心纯属浪费
2楼-- · 2019-05-21 13:40

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
查看更多
叼着烟拽天下
3楼-- · 2019-05-21 13:44

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>
查看更多
淡お忘
4楼-- · 2019-05-21 13:57

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()
查看更多
干净又极端
5楼-- · 2019-05-21 13:59

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>
查看更多
登录 后发表回答