在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
是否存在问题?解决这个问题的最佳方法是什么?
答案 0 :(得分:8)
来自ant manual:
从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>
通知=&gt;属性语法<echo if:true="${@{whentrue}}">
,仅在使用@ {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.
还假设像<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>
输出,没有错误,但宏程序没有执行:
[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功能),提供了一个包含macrodef的代码片段。
- 编辑(2) -
虽然2013年12月29日新版Ant 1.9.3(see releasenotes here)修复了与新if:和unless:属性(https://issues.apache.org/bugzilla/show_bug.cgi?id=55885)相关的错误,但我们的问题仍然存在。因此,我打开了一个错误报告,请参阅ant bug数据库bugid 55971。
- 编辑(3) -
最后找到解决方案。除了Bugid 55885的错误修正之外,Ant版本1.9.3还提供了新的if:和unless的文档的错误修正:和unless:attributes =&gt; 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>