包含Macrodef参数的Ant属性

时间:2009-10-08 13:20:52

标签: ant

有没有办法在包含macrodef参数字符串的属性中放置值?

例如,在下面的macrodef中,我想将 $ {build.dir} / widget / @ {platform} / @ {resolution} / $ {widget.name} 添加到在几个macrodef中使用的属性。

<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>

会变成

<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>

2 个答案:

答案 0 :(得分:3)

在ant 1.8中,你有local任务,专门为此设计。

答案 1 :(得分:1)

根据您的意见,也许这会有所帮助。

您可以定义一个设置widget.base属性的目标,然后调用其名称作为参数传递的另一个目标。在每次调用包装器时,将重新计算widget.base的值。 下面的示例设置了两个嵌套目标,第一个在您的问题中调用macrodef。第二个只是直接调用目标中的任务。主要目标每次调用几次来测试替换。

<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>

运行此脚本的输出是:

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