Ant 蚂蚁:对。将属性设置为true-won';不要使任务执行
在Ant 1.9.1中,您可以在大多数任务上使用 我定义了一个宏,在其中我将尝试运行以下任务:Ant 蚂蚁:对。将属性设置为true-won';不要使任务执行,ant,macros,Ant,Macros,在Ant 1.9.1中,您可以在大多数任务上使用 我定义了一个宏,在其中我将尝试运行以下任务: <property name="test.templates" value="true"/> .... <target name="test.templates" description="Test the autoconfiguration templates and answers"> <test.templates if:tr
<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
是否有问题?解决这个问题的最佳方法是什么?发件人:由于Ant 1.9.1,可以在所有 使用特殊名称空间的任务和嵌套元素 到目前为止,没有在宏定义中使用新的if和除非属性,但以下代码段有效:
<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>
我的另一次尝试:<macrodef name="foobar" if:true="foo">
<attribute name="bla"/>
<sequential>
<echo>@{bla}</echo>
</sequential>
</macrodef>
<echo>${ant.version}</echo>
<foobar bla="yada,yada"/>
在这方面似乎仍存在一些不一致之处,因为此功能是全新的。也许我们应该提交一个bug
--编辑(1)——
刚刚在ant bug数据库中找到了一个(他已经实现了if/除非特性),提供了一个带有宏定义的片段 --编辑(2)——
尽管2013年12月29日发布的新Ant版本1.9.3()修复了一个与新的if:and除非:attributes()相关的错误,但我们的问题仍然存在。因此,我打开了一个bug报告,请参阅ant bug数据库。
--编辑(3)——
最后找到了解决方案。除了Ant版本1.9.3的错误修复之外,还为新的if:and除非:attributes=>的文档提供了错误修复,显示必须使用
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>
....
我注意到,在2013年12月29日发布的新ant版本仍然存在问题后,您有一个名为“test.templates”的目标、属性和宏填充了错误报告-有关详细信息,请参阅我的更新答案编辑(2)。找到了解决方案-请参阅我的更新答案编辑(3)!谢谢你所做的一切!我们使用的是antcontrib的
,但我认为这将是一种更优雅的处理问题的方式,但当它不起作用时,我感到惊讶。@Rebase:如果您编辑此答案,将解决方案放在顶部而不是底部(在几个非解决方案下),那就太好了。如果不是你对这个问题的“我解决了!”评论,我会在结束之前放弃你的答案。
... 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.
<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
<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>