Search code examples
xmlantbuild-automation

Apache Ant won't do math


I'm using distributed source to work on a project in Java, and the source has to be built using a custom Ant script in order to work properly. The problem is, when I run the script, I get

BUILD FAILED
C:\[path]\autobuild.xml:47: Only 1 operation can be specified

Lines 47-59 read:

<math result="months" datatype="int">
    <op op="-">
        <op op="+">
            <num value="${month}"/>

            <op op="*">
                <num value="${year}"/>
                <num value="12"/>
            </op >
        </op>
        <num value="24097"/>
    </op>
</math>

I've tried googling the error phrase and the only result is the Ant source of MathTask.java where it shows that this is what should happen if the operation is null. I tried changing the code along the lines of:

<op type="-">

or

<op op="subtract">

but these produced the same error. I changed it instead to:

<subtract>

and now there is no error but the result is always zero. I've also tried reducing the math stanza to having just one operation:

<math result="months" datatype="int">
    <op op="subtract">
        <num value="27940"/>
        <num value="24123"/>
    </op>
</math>

but the problem remains.

I'm completely at a loss. Does anyone have any clue what's going on here? I'm using the newest version of Ant... ${month} and ${year} are properly set (I've also tested with hardcoded numbers and it still doesn't work).

Thanks!


Solution

  • You can use an embedded scripting language like Jython or Groovy instead, which might be more readable and easier to manipulate than XML.

    <groovy>
    month = Integer.valueOf(properties["month"])
    year = Integer.valueOf(properties["year"])
    
    properties["months"] = 24097 - ((year * 12) + month)
    </groovy>