ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stefan Bodewig <bode...@apache.org>
Subject Re: Macrodef attribute expansion question...
Date Wed, 08 Jul 2009 15:16:20 GMT
On 2009-07-08, Scot P. Floess <sfloess@nc.rr.com> wrote:

> I have a question about attribute expansion for macrodefs...

> Consider the following:

> <project>
> 	<property  name = "myMessage1"  value = "Name1 [@{name}]
> value1 = [@{value}]"/>
> 	<property  file = "build.properties"/>

> 	<macrodef  name = "testOutput">
> 		<attribute  name = "name"/>
> 		<attribute  name = "value"/>

> 		<sequential>
> 			<property  name = "myMessage2"  value = "Name2
> [@{name}]  value2 = [@{value}]"/>
> 			<property  name = "myMessage4"  value =
> "${myMessage1}"/>

> 			<echo  message = "1:  ${myMessage1}"/>
> 			<echo  message = "2:  ${myMessage2}"/>
> 			<echo  message = "3:  Name3 [@{name}]  value3
> = [@{value}]"/>
> 			<echo  message = "4:  ${myMessage4}"/>
> 			<echo  message = "5:  ${myMessage5}"/>
> 		</sequential>
> 	</macrodef>

> 	<testOutput  name = "foo"  value = "bar"/>
> </project>


> build.properties:

> myMessage5=Name5 [@{name}]  value5 = [@{value}]

> When run:

> Buildfile: build.xml
>      [echo] 1:  Name1 [@{name}]  value1 = [@{value}]
>      [echo] 2:  Name2 [foo]  value2 = [bar]
>      [echo] 3:  Name3 [foo]  value3 = [bar]
>      [echo] 4:  Name1 [@{name}]  value1 = [@{value}]
>      [echo] 5:  Name5 [@{name}]  value5 = [@{value}]


> I was "hoping" for myMessage1, myMessage4 and myMessage5 to be
> expanded based upon the attributes in testOutput.

What you see here is that attributes get expanded before properties
when the macrodef is run.  When the forth echo task is run the message
attribute is scanned for @{} first and the result is passed to the
task itself - which does the property expansion (hand-wavingly).  When
${myMessage4} gets expandend it is too late to have attributes
expanded.

The order of expansions is so that you can do things like
${@{propertyname}}
(e.g. http://ant.apache.org/faq.html#propertyvalue-as-name-for-property)
- or maybe it is what it is since we needed a specific order and
that's the one that was chosen.

> The reason I am doing this, is I have some macrodef's and am
> internationalizing messages/error messages - some of which include the
> attributes in the messages.

> Is there a way to force expansion "at the right time?"

This here seems to do what you want:

<project>
  <property  name = "myMessage1"  value = "Name1 [@{name}] value1 = [@{value}]"/>
  <macrodef  name = "testOutput">
    <attribute  name = "name"/>
    <attribute  name = "value"/>
    <attribute name="myMessage6" default="${myMessage1}"/>

    <sequential>
      <echo  message = "6:  @{myMessage6}"/>
    </sequential>
  </macrodef>

  <testOutput  name = "foo"  value = "bar"/>
</project>

$ ant -f ../../Temp/foo.xml
Buildfile: ..\..\Temp\foo.xml
     [echo] 6:  Name1 [foo] value1 = [bar]

BUILD SUCCESSFUL
Total time: 0 seconds

Properties in the default attribute are expanded at macrodef execution
time, not macro-execution time.  And attribute's default attribute can
access the values of attributes defined earlier.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message