Date: 2004-06-02T11:26:57
Editor: 32.102.170.75 <>
Wiki: Ant Wiki
Page: AntOddities
URL: http://wiki.apache.org/ant/AntOddities
Useful macrodef which arose on discussion on user list
Change Log:
------------------------------------------------------------------------------
@@ -400,3 +400,58 @@
BUILD SUCCESSFUL
Total time: 3 seconds
}}}
+
+
+
+
+== Compounding a property name from the instantiations of multiple previously instanced properties
==
+
+=== The Problem ===
+
+Although plain Ant sytax does not allow one to do so, a simple macrodef can derive property
names by pasting together the instantiations of multiple previously instanced properties.
+
+Given a set of resource-like properties such as:
+{{{
+ driver.bsd="SomeBSDDriver"
+ driver.os2="A.Real.Old.Driver"
+ driver.windows="GPFGalore"
+
+ booter.bsd="boot"
+ booter.os2="boot.sys"
+ booter.windows="ntldr"
+}}}
+
+You might wish to pass to some target or task properties/parameters such as ${component}
and ${targetOS}
+in the form
+
+ {{{ <do-something-with object="${${component}.${targetOS}}/> }}}
+
+so that if, for example, ${component} were valued as "driver" and ${targetOS} were valued
as "os2",
+the value of ${${component}.${targetOS}} would be the expansion of ${driver.os2}, i.e., "A.Real.Old.Driver".
+However, Ant expansions of property instantiations are not recursive. So in this instance
the expansion of ${${component}.${targetOS}} is not "A.Real.Old.Driver" but instead undefined
and possibly varying by Ant version. (Ant 1.6.1 would yield a literal value of ${${component}.${targetOS}}
+while Ant 1.7 alpha currently yields ${${component}.os2})
+
+=== Solution Macrodef ===
+
+Define a macro allowing us to express the problem case as:
+
+{{{
+ <macro.compose-property name="object" stem="${component} selector="${targetOS}"/>
+ <do-something-with object="${object}"/>
+}}}
+
+
+Here is the macro (along lines suggested by Peter Reilly with reference to http://ant.apache.org/faq.html#propertyvalue-as-name-for-property):
+
+{{{
+<!-- Allows you define a new property with a value of ${${a}.${b}} which can't be done
by the Property task alone. -->
+ <macrodef name="macro.compose-property">
+ <attribute name="name"/>
+ <attribute name="stem"/>
+ <attribute name="selector"/>
+ <sequential>
+ <property name="@{name}" value="${@{stem}.@{selector}}"/>
+ </sequential>
+ </macrodef>
+
+}}}
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org
|