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

I am trying to execute a large ant build file that is provided to me, in which case I am having trouble understanding the functionality of xmlproperty. 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> 

There is a task in the assembly file that can be simplified for the following example:

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

As I understand it, if there was only one <tag> element, I could get the <file> content using ${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 this is some kind of list, how do I iterate over the <file> values?

I am using ant 1.6.2.

+4
source share
1 answer

If multiple elements have the same name, <xmlproperty> creates a comma-delimited property:

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

Result:

 run: [echo] file1,file2 

To handle comma-separated values, consider using the <for> task from a 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> 

Result:

 run: [echo] file1 [echo] file2 
+9
source

Source: https://habr.com/ru/post/1478959/


All Articles