This site runs best with JavaScript enabled.

Nested properties in Ant

Torsten Uhlmann

April 29, 2008

AGYNAMIX, Blogger, Java, Scala, Javascript, Typescript, ReactJS

Photo by Torsten Uhlmann

englishprogramming

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:

1<!-- Needed to resolve a nested property like ${document.${input.file}.type} -->
2<macrodef name="resolveProperty">
3<attribute name="property" />
4<attribute name="value" />
5<sequential>
6<property name="tmp1.@{property}" value="document.@{value}.type" />
7<resolveProperty2 property="@{property}" value="${tmp1.@{property}}" />
8</sequential>
9</macrodef>
10
11<macrodef name="resolveProperty2">
12<attribute name="property" />
13<attribute name="value" />
14<sequential>
15<property name="@{property}" value="${@{value}}" />
16</sequential>
17</macrodef>

Now I can set my property like so:

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

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

Nice.

Share article