【问题标题】:Ant if:true. Setting property to true won't cause the task to executed蚂蚁如果:真。将属性设置为 true 不会导致任务执行
【发布时间】:2013-11-20 17:20:10
【问题描述】:

在 Ant 1.9.1 中,您可以在大多数任务中使用 if and unless attributes

我已经定义了一个宏,我试图在其中运行这些任务:

<property name="test.templates"     value="true"/>
....
<target name="test.templates"
    description="Test the autoconfiguration templates and answers">
    <test.templates
        if:true="test.templates"
        template.root.dir="${main.dir}"
        answers.dir="${main.config.dir}"/>
</target>

但是,这不会运行我的宏——即使属性 test.templates 设置为 true。如果我删除该行,我的 test.template 宏将起作用。

在用户定义的宏中使用if:true 是否有问题?解决此问题的最佳方法是什么?

【问题讨论】:

  • 我注意到您有一个目标、属性和宏都称为“test.templates”
  • 在 2013 年 12 月 29 日的新 ant 版本中仍然存在问题后填写了错误报告 - 有关详细信息,请参阅我的更新答案编辑 (2)。
  • 找到解决方案 - 请参阅我的更新答案编辑(3)!

标签: ant macros


【解决方案1】:

来自ant manual

从 Ant 1.9.1 开始,可以在所有属性上添加 if 和 unless 属性 使用特殊命名空间的任务和嵌套元素。

直到现在还没有在宏定义中使用新的 if 和 unless 属性,但以下 sn-p 有效:

<project xmlns:if="ant:if" xmlns:unless="ant:unless">

    <property name="foo" value="true"/>

    <macrodef name="foobar">
     <attribute name="bla"/>
     <attribute name="whentrue"/>
     <sequential>
       <echo if:true="${@{whentrue}}">@{bla}</echo>
     </sequential>
    </macrodef>

     <echo>${ant.version}</echo>
     <foobar whentrue="foo" bla="yada,yada"/>

    </project>

注意 => 属性语法&lt;echo if:true="${@{whentrue}}"&gt;,仅在使用@{whentrue} 时不起作用。

输出:

 [echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
 [echo] yada,yada

我的另一个尝试:

<macrodef name="foobar" if:true="foo">
 <attribute name="bla"/>
 <sequential>
   <echo>@{bla}</echo>
 </sequential>
</macrodef>

 <echo>${ant.version}</echo>
 <foobar bla="yada,yada"/>

没用:

... Problem: failed to create task or type foobar
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.

还假设像 &lt;foobar bla="yada,yada" if:true="foo"/&gt; 这样的东西会起作用:

<project xmlns:if="ant:if" xmlns:unless="ant:unless">

  <property name="foo" value="true"/>

  <macrodef name="foobar">
   <attribute name="bla"/>
   <sequential>
     <echo>@{bla}</echo>
   </sequential>
  </macrodef>

  <echo>${ant.version}</echo>
  <foobar bla="yada,yada" if:true="foo"/>

</project>

输出,没有错误但没有执行宏定义:

[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
BUILD SUCCESSFUL

似乎该区域仍有一些不一致之处,因为此功能是全新的。
也许我们应该提交一个错误!?
-- 编辑 (1) --
刚刚在 ant bug 数据库中找到了一个comment from 2007 by Peter Reilly(他实现了 if / unless 功能),提供了一个带有宏定义的 sn-p。

-- 编辑 (2) --
尽管 2013 年 12 月 29 日发布的新 Ant 1.9.3 (see releasenotes here) 修复了与新 if: and unless: 属性 (https://issues.apache.org/bugzilla/show_bug.cgi?id=55885) 相关的错误,但我们的问题仍然存在。因此我打开了一个错误报告,请参阅 ant 错误数据库 bugid 55971

-- 编辑 (3) --
终于找到了解决办法。除了Bugid 55885 的错误修复,Ant Release 1.9.3 还为新的 if: and unless: attributes => Bugid 55359 的文档提供了错误修复,表明必须使用 if:true="${propertyname}" 而不是 if:true="propertyname"。 因此,在 Ant 1.9.3 上升级后,您的宏应该可以正常工作:

<property name="test.templates"     value="true"/>
....
<target name="test.templates"
 description="Test the autoconfiguration templates and answers">
  <test.templates
   if:true="${test.templates}"
   template.root.dir="${main.dir}"
   answers.dir="${main.config.dir}"/>
</target>

【讨论】:

  • 感谢您所做的所有工作!我们使用了 antcontrib 的 &lt;if/&gt;,但我认为这将是一种更优雅的处理问题的方式,但当它不起作用时,我感到很惊讶。
  • @Rebse:如果您编辑此答案以将解决方案放在顶部而不是底部(在几个非解决方案下),那就太好了。如果不是因为您的“我解决了它!”评论这个问题,我会在结束之前放弃你的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 2011-12-29
  • 2011-10-27
  • 1970-01-01
  • 2015-05-30
  • 2012-10-09
相关资源
最近更新 更多