Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant xmlproperty task. What happens when there is more than one tag with the same name?

Tags:

xml

ant

I am trying to follow a large ant buildfile that I have been given, and I am having trouble understanding the functionality of xmlproperty in this case. Consider this xml file, example.xml.

<main>
  <tagList>
    <tag>
      <file>file1</file>
      <machine>machine1</machine>
    </tag>
    <tag>
      <file>file2</file>
      <machine>machine2</machine>
    </tag>
  </tagList>
</main>

In the buildfile, there is a task which can be simplified to the following for this example:

<xmlproperty file="example.xml" prefix="PREFIX" />

As I understand it, if there was only one <tag> element, I could get the contents of <file> with ${PREFIX.main.tagList.tag.file} because it is roughly equivalent to writing this:

<property name="PREFIX.main.tagList.tag.file" value="file1"/>

But as there are two <tag>s, what is the value of ${PREFIX.main.tagList.tag.file} in this case? If it is some sort of list, how do I iterate through both <file> values?

I am using ant 1.6.2.

like image 989
Matt Hyde Avatar asked May 03 '13 10:05

Matt Hyde


People also ask

What is true about ant tasks?

Ant tasks are the units of your Ant build script that actually execute the build operations for your project. Ant tasks are usually embedded inside Ant targets. Thus, when you tell Ant to run a specific target it runs all Ant tasks nested inside that target.

What is the default target in ant?

the default target to use when no target is supplied. No; however, since Ant 1.6. 0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.

What is Ant build XML?

Ant uses an xml file for its configuration. The default file name is build. xml . Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc.


1 Answers

When multiple elements have the same name, <xmlproperty> creates a property with comma-separated values:

<project name="ant-xmlproperty-with-multiple-matching-elements" default="run" basedir=".">
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <echo>${PREFIX.main.tagList.tag.file}</echo>
    </target>
</project>

The result:

run:
     [echo] file1,file2

To process the comma-separated values, consider using the <for> task from the third-party Ant-Contrib library:

<project 
    name="ant-xmlproperty-with-multiple-matching-elements" 
    default="run" 
    basedir="." 
    xmlns:ac="antlib:net.sf.antcontrib"
    >
    <taskdef resource="net/sf/antcontrib/antlib.xml" />
    <target name="run">
        <xmlproperty file="example.xml" prefix="PREFIX" />

        <ac:for list="${PREFIX.main.tagList.tag.file}" param="file">
            <sequential>
                <echo>@{file}</echo>
            </sequential>
        </ac:for>
    </target>
</project>

The result:

run:
     [echo] file1
     [echo] file2
like image 130
Chad Nouis Avatar answered Nov 06 '22 19:11

Chad Nouis