tags:

views:

667

answers:

2

Is there a way to put a value in a property that contains a macrodef parameter string?

For example, in the following macrodef I would like to add the ${build.dir}/widget/@{platform}/@{resolution}/${widget.name} to a property as it is used in several macrodefs.

<macrodef name="setBuildstamp">
    <attribute name="platform" />
    <attribute name="resolution" />
    <sequential>
      <replace file="${build.dir}/widget/@{platform}/@{resolution}/${widget.name}/Contents/Javascript/views/sidebar/DevSettingsView.js" token="%%%BUILD_TIMESTAMP%%%" value="${build.timestamp}" />
    </sequential>
</macrodef>

would become

<property name="widget.base" value"${build.dir}/widget/@{platform}/@{resolution}/${widget.name}" />

<macrodef name="setBuildstamp">
    <attribute name="platform" />
    <attribute name="resolution" />
    <sequential>
         <replace file="${widget.base}/Contents/Javascript/views/sidebar/DevSettingsView.js" token="%%%BUILD_TIMESTAMP%%%" value="${build.timestamp}" />
    </sequential>
</macrodef>
+1  A: 

Based on your comments, perhaps this will help.

You can define a target that sets the widget.base property, and in turn calls another target the name of which is passed as a parameter. The value of widget.base will be recalculated within each call to the wrapper. The example below sets up two nested targets, the first calls a macrodef as in your question. The second simply invokes the tasks in the target directly. The main target calls each a couple of times to test the substitution.

<property name="platform" value="foo" />
<property name="resolution" value="bar" />
<property name="widget.name" value="bibble" />
<property name="build.dir" value="boo" />
<property name="build.timestamp" value ="1234"/>

<!--sets the property on each call then calls the target with the passed name -->
<target name="wrapper">
    <property name="widget.base" value="${build.dir}/widget/${platform}/${resolution}/${widget.name}" />

    <antcall target="${nestedTarget}"/>
</target>

<target name="nestedTarget1">
    <setBuildstamp  widgetBase="${widget.base}" buildTimestamp="${build.timestamp}"/>
</target>

<target name="nestedTarget2">
    <echo>"different/${widget.base}/Contents/Javascript/views/sidebar/DevSettingsView.js" 
        token="%%%BUILD_TIMESTAMP%%%" value="${build.timestamp}"</echo>
</target>

<macrodef name="setBuildstamp">
    <attribute name="widgetBase" />
    <attribute name="buildTimestamp" />
    <sequential>
      <echo>"@{widgetBase}/Contents/Javascript/views/sidebar/DevSettingsView.js" 
        token="%%%BUILD_TIMESTAMP%%%" value="@{buildTimestamp}"</echo>
    </sequential>
</macrodef>

<target name="test">
  <antcall target="wrapper">
    <param name="platform" value="${platform}"/>
    <param name="resolution" value="${resolution}"/>
    <param name="nestedTarget" value="nestedTarget1"/>
  </antcall>
  <antcall target="wrapper">
    <param name="platform" value="starsky"/>
    <param name="resolution" value="hutch"/>
    <param name="nestedTarget" value="nestedTarget1"/>
  </antcall>
  <antcall target="wrapper">
    <param name="platform" value="fizz"/>
    <param name="resolution" value="buzz"/>
    <param name="nestedTarget" value="nestedTarget2"/>
  </antcall>
  <antcall target="wrapper">
    <param name="platform" value="jim"/>
    <param name="resolution" value="joe"/>
    <param name="nestedTarget" value="nestedTarget2"/>
  </antcall>
</target>

The output of running this script is:

test:

wrapper:

nestedTarget1:
     [echo] "boo/widget/foo/bar/bibble/Contents/Javascript/views/sidebar/DevSettingsView.js"
     [echo]             token="%%%BUILD_TIMESTAMP%%%" value="1234"

wrapper:

nestedTarget1:
     [echo] "boo/widget/starsky/hutch/bibble/Contents/Javascript/views/sidebar/DevSettingsView.js"
     [echo]             token="%%%BUILD_TIMESTAMP%%%" value="1234"

wrapper:

nestedTarget2:
     [echo] "different/boo/widget/fizz/buzz/bibble/Contents/Javascript/views/sidebar/DevSettingsView.js"
     [echo]             token="%%%BUILD_TIMESTAMP%%%" value="1234"

wrapper:

nestedTarget2:
     [echo] "different/boo/widget/jim/joe/bibble/Contents/Javascript/views/sidebar/DevSettingsView.js"
     [echo]             token="%%%BUILD_TIMESTAMP%%%" value="1234"

BUILD SUCCESSFUL
Rich Seller
I specifically used macrodefs because I needed the ability to vary the path's depending on which targets are selected. If I set widget.base (being immutable) I will not be able to change this once it is set. I was hoping though that there was a way to extract the paths in the macrodefs, much like I would put a path in a property, but it appears this will no work when the @{} attribute selector is present.
Steve
+1  A: 

In ant 1.8, you've got the local task, which was designed specifically for this.

oenli
Thanks. Good to know.
Steve