From ant manual :
Since Ant 1.9.1 it is possible to add if and unless attributes on all
tasks and nested elements using special namespaces.
Didn't use the new if and unless attributes in a macrodef until now, but the following snippet works :
<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>
Notice => the property syntax <echo if:true="${@{whentrue}}">
, it doesn't work when using @{whentrue} only.
Output :
[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
[echo] yada,yada
My other try :
<macrodef name="foobar" if:true="foo">
<attribute name="bla"/>
<sequential>
<echo>@{bla}</echo>
</sequential>
</macrodef>
<echo>${ant.version}</echo>
<foobar bla="yada,yada"/>
didn't work :
... 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.
Also assumed that something like <foobar bla="yada,yada" if:true="foo"/>
would work :
<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>
output, no error but macrodef doesn't get executed :
[echo] Apache Ant(TM) version 1.9.1 compiled on May 15 2013
BUILD SUCCESSFUL
Seems like there are still some inconsistencies in that area, as this feature is spanking new.
Maybe we should file a bug !?
-- EDIT (1) --
Just found a comment from 2007 by Peter Reilly (he has implemented the if / unless feature) in the ant bug database providing a snippet with a macrodef.
-- EDIT (2) --
Although the new Ant release 1.9.3 from December 29, 2013 (see releasenotes here) fixed a bug related to the new if: and unless: attributes (https://issues.apache.org/bugzilla/show_bug.cgi?id=55885) our problem still remains. Thus i opened a bug report, see ant bug database bugid 55971.
-- EDIT (3) --
Finally the solution is found. Beside the bugfix for Bugid 55885 the Ant Release 1.9.3 provides also a bugfix for the documentation of the new if: and unless: attributes => Bugid 55359 showing that if:true="${propertyname}"
instead of if:true="propertyname"
has to be used.
So your macro should work after upgrade on Ant 1.9.3 like that :
<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>