Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>thiagolsilva,</p> <p>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.</p> <p>The sample data projects.xml file from the example above:</p> <pre><code>&lt;projects&gt; &lt;project&gt; &lt;name&gt;Project 1&lt;/name&gt; &lt;url&gt;//someurl1&lt;/url&gt; &lt;package&gt;project1&lt;/package&gt; &lt;/project&gt; &lt;project&gt; &lt;name&gt;Project 2&lt;/name&gt; &lt;url&gt;//someurl2&lt;/url&gt; &lt;package&gt;project2&lt;/package&gt; &lt;/project&gt; &lt;/projects&gt; </code></pre> <p>Has to be transformed into following format of projects.xml file:</p> <pre><code>&lt;myProjects&gt; &lt;!-- name;url;package --&gt; &lt;myProject&gt;Project 1;//someurl1;project1&lt;/myProject&gt; &lt;myProject&gt;Project 2;//someurl2;project2&lt;/myProject&gt; &lt;/myProjects&gt; </code></pre> <p>Here, the previous node elements were transformed into a single string with fields separated by ‘;’ character. </p> <p>I have renamed the element “project” to “myProject” to avoid confusion, as I will be using ant “project” object in javascript.</p> <p>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.</p> <pre><code>&lt;project name="testProjects" default="ProcessProjects" basedir="."&gt; &lt;target name="init" &gt; &lt;!-- 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. --&gt; &lt;taskdef resource="net/sf/antcontrib/antlib.xml"&gt; &lt;classpath&gt;&lt;pathelement location="./lib/ant-contrib-1.0b3.jar"/&gt;&lt;/classpath&gt; &lt;/taskdef&gt; &lt;xmlproperty file="./projects.xml" keepRoot="false"/&gt; &lt;!-- Properties will be used to store the values of: name;url;package --&gt; &lt;property name="name" value=""/&gt; &lt;property name="url" value=""/&gt; &lt;property name="package" value=""/&gt; &lt;/target&gt; &lt;target name="ProcessProjects" depends="init"&gt; &lt;echo&gt;Process Projects&lt;/echo&gt; &lt;foreach list="${myProject}" target="processLoopStep" param="var1"/&gt; &lt;/target&gt; &lt;target name="processLoopStep" &gt; &lt;script language="javascript"&gt; &lt;![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]); ]]&gt; &lt;/script&gt; &lt;!-- Do what you like with the values of the &lt;project&gt; element --&gt; &lt;echo message="myProject name = ${name} url = ${url} package = ${package}"/&gt; &lt;/target&gt; &lt;/project&gt; </code></pre> <p>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</p> <p>The output:</p> <pre><code>C:\temp&gt;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 </code></pre> <p>Regards, Jurek</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload