Nested properties in Ant

by tuhlmann on April 29, 2008 Posted in Programming,english

Today while scripting my Dopus Ant files I came across a problem with nested properties.

Something of the sort ${document.${input.file}.type}.

Turns out that Ant cannot do this by default. Yet I found a blog entry that explains how it can be done with newer versions of Ant (those that support macros). I adapted the solution a little bit with the following result:

<!– Needed to resolve a nested property like ${document.${input.file}.type} –>
<macrodef name="resolveProperty">
    <attribute name="property" />
    <attribute name="value" />
    <sequential>
      <property name="tmp1.@{property}" value="document.@{value}.type" />
      <resolveProperty2 property="@{property}" value="${tmp1.@{property}}" />
    </sequential>
</macrodef>

<macrodef name="resolveProperty2">
  <attribute name="property" />
  <attribute name="value" />
  <sequential>
    <property name="@{property}" value="${@{value}}" />
  </sequential>
</macrodef>

Now I can set my property like so:

<resolveProperty property="document.calculated.type" value="${project.input.file}" />

which effectively sets document.calculated.type=${document.${project.input.file}.type}.

Nice.

{ 5 comments… read them below or add one }

Torsten Herrmann March 27, 2009 at 1:16 pm

thx, this was very helpfully.

Vladimir T April 28, 2009 at 1:50 pm

Thanks a lot!

bentzy sagiv June 4, 2009 at 5:52 pm

I doesn’t fit my needs, but the following works great!

at build properties:
OLDPROP=AB
ABCD=itworks

at build.xml:

at log:

[echo] itworks
:-)

–bentzy

bentzy sagiv June 4, 2009 at 5:56 pm

at build.xml:

bentzy sagiv June 4, 2009 at 5:58 pm

this site deleted my sample – you have to use propertycopy ant-contrib task

Leave a Comment

Previous post:

Next post:

design by rowboat media using thesis