Like <foreach> in <macrodef>?

I have xml as below:

<data> <foo>value1</foo> <foo>value2</foo> <foo>value3</foo> </data> 

I want to create a macrodef that implements the functions below:

 <?xml version="1.0"?> <project name="OATS" default="execute" basedir="."> <xmlproperty file="data.xml" collapseAttributes="true"/> <target name="execute"> <foreach list="${data.foo}" target="runScript" param="script"/> </target> <target name="runScript"> <echo>Doing things with ${script}</echo> </target> </project> 

Does anyone know how to do this? Thanks in advance.

+4
source share
3 answers

xmltask is the best choice in the Ant community for this purpose, t should define your own macrodef.

So for example:

  <tools:xmltask source="data.xml" report="false" > <tools:call path="data/foo"> <param name="value" path="text()"/> <actions> <echo>Doing things with @{value}</echo> </actions> </tools:call> </tools:xmltask> 

I recommend that you read the user manual as xmltask has many options. It basically supports XPath to extract and iterate any part of your xml. It also supports calls to existing targets in addition to anonymous blocks of code (as in the example).

It’s just hard to win.

+3
source

The following example uses the groovy ANT task

 <project name="OATS" default="execute" basedir="."> <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"> <classpath> <pathelement location="lib/groovy-all-2.1.0-rc-2.jar"/> </classpath> </taskdef> <target name="execute"> <groovy> def data = new XmlSlurper().parse(new File("data.xml")) data.foo.each { properties["script"] = it ant.project.executeTarget("runScript") } </groovy> </target> <target name="runScript"> <echo>Doing things with ${script}</echo> </target> </project> 
0
source

This is my macrodef.

  <?xml version="1.0" encoding="UTF-8"?> <project name="OATS" default="test" basedir="."> <property environment = "env"/> <path id = "antcontrib.path"> <fileset file = "${env.ANT_HOME}/../net.sf.antcontrib_1.1.0.0_1-0b2/lib/ant-contrib.jar"/> </path> <taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="antcontrib.path"/> <macrodef name="runOATS"> <attribute name="suite"/> <attribute name="toDir"/> <sequential> <delete dir="@{toDir}"/> <mkdir dir="@{toDir}"/> <xmlproperty file="@{suite}" collapseAttributes="true"/> <for list="${data.foo}" param="script"> <sequential> <runScript script="@{script}"/> </sequential> </for> </sequential> </macrodef> <macrodef name="runScript"> <attribute name="script"/> <sequential> <echo>Doing things with @{script}</echo> </sequential> </macrodef> <target name="test"> <runOATS toDir="/OATS/results" suite="data.xml"/> </target> </project> 
0
source

All Articles