Apache Ant 不会做数学

发布于 2024-12-14 12:33:03 字数 1364 浏览 2 评论 0原文

我正在使用分布式源代码来处理 Java 项目,并且必须使用自定义 Ant 脚本构建源代码才能正常工作。问题是,当我运行脚本时,我读到

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

第 47-59 行:

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

我尝试用谷歌搜索错误短语,唯一的结果是 MathTask.java 的 Ant 源码它表明如果操作为空,这就是应该发生的情况。我尝试按照以下方式更改代码:

<op type="-">

或者

<op op="subtract">

但这些产生了相同的错误。我将其更改为:

<subtract>

现在没有错误,但结果始终为零。 我还尝试将数学节减少到只有一个运算:

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

但问题仍然存在。

我完全不知所措。有人知道这里发生了什么事吗?我正在使用最新版本的 Ant... ${month} 和 ${year} 已正确设置(我也用硬编码数字进行了测试,但它仍然不起作用)。

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-12-21 12:33:03

我对这段代码也有同样的问题。有趣的是,大多数时候它确实有效。大约每 5 次我运行构建脚本时就会发生该错误。

<math datatype="int" result="rndNum">
    <op op="+">
        <num value="42000" />
        <op op="*">
           <num value="1000"/>
           <op op="random"/>
        </op>               
    </op>
</math>

我从数学任务中删除了datatype属性并将其放在第一个操作中。

<math result="rndNum">
    <op datatype="int" op="+">
        <num value="42000" />
        <op op="*">
           <num value="1000"/>
           <op op="random"/>
        </op>               
    </op>
</math>

到目前为止它有效。让我们看看要花多长时间:)如果它不起作用,我会删除我的答案。

I had the same problem with this piece of code. The funny thing is that most of the time it does work. The error occured about every 5th time I ran my build script.

<math datatype="int" result="rndNum">
    <op op="+">
        <num value="42000" />
        <op op="*">
           <num value="1000"/>
           <op op="random"/>
        </op>               
    </op>
</math>

I removed the datatype attribute from the math task and put it in the first op.

<math result="rndNum">
    <op datatype="int" op="+">
        <num value="42000" />
        <op op="*">
           <num value="1000"/>
           <op op="random"/>
        </op>               
    </op>
</math>

So far it works. Lets see how long :) I'll remove my answer if it doesn't work.

与往事干杯 2024-12-21 12:33:03

您不需要额外的 ant 任务或额外的脚本语言,只需使用 java 附带的内置 javascript 脚本引擎(自 jdk 1.6 起,Sun 自己基于 rhino 1.6R2 的实现)并放入宏定义以供重用,即:

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

如果您需要覆盖现有的用户属性(=通过 ant -f foobar.xml -Dmyuserproperty=foo ... 在命令行上定义的那些属性),您必须使用方法

project.setUserProperty()

You need no additional ant tasks or additional scripting languages, just use the builtin javascript scripting engine java ships with (since jdk 1.6, Sun's own implementation based on rhino 1.6R2) 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()
七分※倦醒 2024-12-21 12:33:03

您可以使用嵌入式脚本语言,例如 JythonGroovy 相反,它可能比 XML 更具可读性和更容易操作。

<groovy>
month = Integer.valueOf(properties["month"])
year = Integer.valueOf(properties["year"])

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文