ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scot P. Floess" <sflo...@nc.rr.com>
Subject Re: Macrodef attribute expansion question...
Date Wed, 08 Jul 2009 16:59:15 GMT

OK, I found my problem...

Apparently as I was refactoring, I moved my <property file = ""/> 
statement to the bottom of my XML file containing macrodefs...

The aforementioned macrodef's had references to the properties contained 
in the file :(

Anyway, I moved the <property file=""/> up above my macrodef's and it 
worked...

Funny thing is, at one point this all worked ;)

Anyway - Stefan - thanks for your help!

On Wed, 8 Jul 2009, Scot P. Floess wrote:

>
> Hey thanks!!!!!
>
> OK, now I feel like a complete idiot!
>
> So, in my code I am doing this exactly - or so I think ;)
>
> Like a "bull in a china shop" I went ahead and did this sample app...  Now 
> looking at your example, and going back to my code I see where I have an 
> error-msg attribute that defaults to a property...
>
> Now on to figuring out what I am "really" doing wrong ;)
>
> Thanks again!
>
>
>
> On Wed, 8 Jul 2009, Stefan Bodewig wrote:
>
>> 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
>> 
>> 
>
> Scot P. Floess
> 27 Lake Royale
> Louisburg, NC  27549
>
> 252-478-8087 (Home)
> 919-890-8117 (Work)
>
> Chief Architect JPlate   http://sourceforge.net/projects/jplate
> Chief Architect JavaPIM  http://sourceforge.net/projects/javapim
>
> Architect Keros          http://sourceforge.net/projects/keros
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>
>

Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-890-8117 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim

Architect Keros          http://sourceforge.net/projects/keros

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


Mime
View raw message