Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

macros - Ant if:true. Setting property to true won't cause the task to executed

In Ant 1.9.1, you have the ability to use if and unless attributes on most tasks.

I have a macro I've defined where I'm trying to run these tasks:

<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>

However, this won't run my Macro -- even though the property test.templates is set to true. If I remove that line, my test.template macro will work.

Is there a problem using if:true in a user defined Macro? What's the best way to get around this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...