ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Reilly <peterrei...@apache.org>
Subject Re: Ant Contrib <if> and <macrodef> question
Date Thu, 02 Jun 2005 16:40:26 GMT
Your macro is incorrect as the line

<property name="temp.checkIsSet" value="@{checkIsSet}" />
will cause the property "temp.checkIsSet" to be set always.

I am not quite sure exactly what you want to do but
the following macro should be it:

<macrodef name="setFromPropertyOrDefault">
   <attribute name="varName"/>
   <attribute name="default"/>
   <attribute name="property"/>
   <sequential>
      <ac:if>
         <isset property="@{property}"/>
         <then>
            <ac:var name="@{varName}" value="${@{property}}"/>
         </then>
         <else>
            <ac:var name="@{varName}" value="@{default}"/>
         </else>
      </ac:if>
    </sequential>
</macrodef>

<setFromPropertyOrDefault  varName="testVar" default="a default" property="not_present"/>
<echo>testVar is ${testVar}</echo>
<property name="present" value="a present property"/>
<setFromPropertyOrDefault  varName="testVar" default="a default" property="present"/>
<echo>testVar is ${testVar}</echo>


Brent Bain wrote:

>Hello:
>
>I'm trying to write a macro to check if a property has been set in a xml
>property file.  If it has not been set then I want to give it a default
>value.  This is running in a loop (using the xmltask's call) to create db
>property files for several different environments.  The loop works great
>after some minor tweaking (thanks Brian!) but I've hit another stumbling
>block.
> 
>My macro looks like this:
> <macrodef name="replaceDefaults">
>  <attribute name="varName" />
>  <attribute name="currentDefault" />
>  <attribute name="checkIsSet" />
>  <sequential>
>   <echo message="checkIsSet: @{checkIsSet}" />
>   <property name="temp.checkIsSet" value="@{checkIsSet}" />
>   <if>
>    <isset property="temp.checkIsSet" />
>    <then>
>     <echo message="isset" />
>     <var name="@{varName}" value="@{checkIsSet}" />
>    </then>
>    <else>
>     <echo message="else" />
>     <var name="@{varName}" value="@{currentDefault}" />
>    </else>
>   </if>
>  </sequential>
> </macrodef>
> 
>I've tried this several different ways and keep hitting what I think is an
>error between the macrodef @{property} and how the ant contrib's <if>
>handles the <isset> condition.
> 
>If I call it like this:
><property name="temp.dbServer" value="dbServer2" />
><replaceDefaults varName="testVar" currentDefault="dbServer1"
>checkIsSet="${temp.dbServer}" />
>It correctly enters the <then> and sets the testVar = dbServer2
> 
>If I call it like this (NOT setting the property first):
><replaceDefaults varName="testVar" currentDefault="dbServer1"
>checkIsSet="${temp.dbServer}" />
>It still enters the <then>
> 
>If I remove the line <property name="temp.checkIsSet" value="@{checkIsSet}"
>/> and then also change <isset property="checkIsSet" /> then it always
>enteres the <else>
> 
>Is there something obviously wrong that I'm doing or is this just a
>"limitation" of the contrib task and macrodef?  Is there some more obvious
>way to set properties (I'm using ant contrib's <var>'s so I can do my
>looping) with defaults??
> 
>Any tips would be greatly appreciated!
>Thanks,
>Brent
> 
>
>  
>


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


Mime
View raw message