I´ve a Xml file with the struct below. I want to read the nod by node and call a specific task with the values without commom separator.In Ant this is possible ?
<
thiagolsilva,
Using a combination of xmlproperty and javascript I was able to split a multi-element (same node) of a projects.xml file and use project node data fields in ant. All actions are performed in a single ant file, in a single execution.
The sample data projects.xml file from the example above:
<projects>
<project>
<name>Project 1</name>
<url>//someurl1</url>
<package>project1</package>
</project>
<project>
<name>Project 2</name>
<url>//someurl2</url>
<package>project2</package>
</project>
</projects>
Has to be transformed into following format of projects.xml file:
<myProjects>
<!-- name;url;package -->
<myProject>Project 1;//someurl1;project1</myProject>
<myProject>Project 2;//someurl2;project2</myProject>
</myProjects>
Here, the previous node elements were transformed into a single string with fields separated by ‘;’ character.
I have renamed the element “project” to “myProject” to avoid confusion, as I will be using ant “project” object in javascript.
Once we have flattened the repeatable xml node into a string, we can use the following ant script to iterate through the elements, read the flattened properties and use them as we wish.
<project name="testProjects" default="ProcessProjects" basedir=".">
<target name="init" >
<!-- From: http://sourceforge.net/projects/ant-contrib/files/ant-contrib/
download the jar and put it a created "lib" folder of your project, where ant file is.
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath><pathelement location="./lib/ant-contrib-1.0b3.jar"/></classpath>
</taskdef>
<xmlproperty file="./projects.xml" keepRoot="false"/>
<!-- Properties will be used to store the values of: name;url;package -->
<property name="name" value=""/>
<property name="url" value=""/>
<property name="package" value=""/>
</target>
<target name="ProcessProjects" depends="init">
<echo>Process Projects</echo>
<foreach list="${myProject}" target="processLoopStep" param="var1"/>
</target>
<target name="processLoopStep" >
<script language="javascript"> <![CDATA[
var var1 = project.getProperty("var1");
var parts = var1.split(";");
project.setProperty("name",parts[0]);
project.setProperty("url",parts[1]);
project.setProperty("package",parts[2]);
]]>
</script>
<!-- Do what you like with the values of the <project> element -->
<echo message="myProject name = ${name} url = ${url} package = ${package}"/>
</target>
</project>
The files in: - C:\temp\lib\ProcessProjects.xml - the build - C:\temp\lib\projects.xml - the data - C:\temp\lib\ant-contrib-1.0b3.jar - the lib jar
The output:
C:\temp>ant -f ProcessProjects.xml
Buildfile: C:\temp\ProcessProjects.xml
init:
ProcessProjects:
[echo] Process Projects
processLoopStep:
[echo] myProject name = Project 1 url = //someurl1 package = project1
processLoopStep:
[echo] myProject name = Project 2 url = //someurl2 package = project2
BUILD SUCCESSFUL
Total time: 0 seconds
Regards, Jurek
Ant projects cannot be nested within the same Ant build file. However, build logic can be modularized using MacroDefs. In addition, subprojects can have their own build files, which may be called from a master build file using the Ant Ant task. Ant targets may be called within the same build file using the AntCall task.
Use the XSLT task to process the input XML file into an ANT script that is subsequently executed.
|-- build.xml
|-- projects-process.xsl
`-- projects.xml
Running will process the information in the
$ ant
Buildfile: /home/mark/tmp/build.xml
run-projects:
[xslt] Processing /home/mark/tmp/projects.xml to /home/mark/tmp/build-tmp.xml
[xslt] Loading stylesheet /home/mark/tmp/projects-process.xsl
build:
dosomething:
[echo] DOSOMETHING: 'Project 1' 'http://someurl1' 'project1'"
dosomething:
[echo] DOSOMETHING: 'Project 2' 'http://someurl2' 'project2'"
BUILD SUCCESSFUL
<projects>
<project>
<name>Project 1</name>
<url>http://someurl1</url>
<package>project1</package>
</project>
<project>
<name>Project 2</name>
<url>http://someurl2</url>
<package>project2</package>
</project>
</projects>
This XSLT stylesheet is used to generate an ANT script
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<project name="genbuild" default="build">
<target name="build">
<xsl:apply-templates select="projects/project"/>
</target>
<target name="dosomething">
<echo>DOSOMETHING: '${name}' '${url}' '${package}'"</echo>
</target>
</project>
</xsl:template>
<xsl:template match="project">
<antcall target="dosomething">
<param name="name" value="{name}"/>
<param name="url" value="{url}"/>
<param name="package" value="{package}"/>
</antcall>
</xsl:template>
</xsl:stylesheet>
Runs the XSLT transform to process the projects.xml file and generate an ANT build file
<project name="demo" default="run-projects">
<target name="run-projects">
<xslt style="projects-process.xsl" in="projects.xml" out="build-tmp.xml"/>
<ant antfile="build-tmp.xml"/>
</target>
<target name="clean">
<delete file="build-tmp.xml"/>
</target>
</project>