Ant if:true.将property设置为true不会导致任务执行

Dav*_* W. 6 ant macros

在Ant 1.9.1中,您可以在大多数任务中使用if和unless属性.

我有一个我定义的宏,我正在尝试运行这些任务:

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

Reb*_*bse 8

来自蚂蚁手册:

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

到目前为止,没有在macrodef中使用新的if和unless属性,但以下代码段有效:

<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>
Run Code Online (Sandbox Code Playgroud)

注意=>属性语法<echo if:true="${@{whentrue}}">,仅在使用@ {whentrue}时不起作用.

输出:

 [echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
 [echo] yada,yada
Run Code Online (Sandbox Code Playgroud)

我的另一个尝试:

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

 <echo>${ant.version}</echo>
 <foobar bla="yada,yada"/>
Run Code Online (Sandbox Code Playgroud)

不起作用:

... 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.
Run Code Online (Sandbox Code Playgroud)

还假设类似的东西<foobar bla="yada,yada" if:true="foo"/>会起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

输出,没有错误,但宏程序没有执行:

[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
BUILD SUCCESSFUL
Run Code Online (Sandbox Code Playgroud)

似乎该领域仍然存在一些不一致之处,因为这个功能正在崭露头角.
也许我们应该提交一个错误!
- 编辑(1) -
刚刚在ant bug中找到了一条评论,在2007年由Peter Reilly发布了一条评论(他已经实现了if/unless特性),提供了一个包含macrodef的代码片段.

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

- 编辑(3) -
最后找到解决方案.除了Bugid 55885的错误修复之外,Ant版本1.9.3还提供了新的if:和除非: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>
Run Code Online (Sandbox Code Playgroud)