Ant xmlproperty任务。当有多个具有相同名称的标签时会发生什么?

时间:2013-05-03 10:18:28

标签: xml ant

我正在尝试遵循我给出的大型ant构建文件,在这种情况下我无法理解xmlproperty的功能。 考虑这个xml文件example.xml。

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

在构建文件中,有一个任务可以简化为以下示例:

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

据我了解,如果只有一个<tag>元素,我可以使用<file>获取${PREFIX.main.tagList.tag.file}的内容 因为它大致相当于写这个:

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

但由于有两个<tag>,在这种情况下${PREFIX.main.tagList.tag.file}的价值是多少?如果它是某种列表,我如何迭代两个<file>值?

我正在使用ant 1.6.2。

1 个答案:

答案 0 :(得分:10)

当多个元素具有相同名称时,<xmlproperty>会创建一个逗号分隔值的属性:

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

结果:

run:
     [echo] file1,file2

要处理以逗号分隔的值,请考虑使用第三方Ant-Contrib库中的the <for> task

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

结果:

run:
     [echo] file1
     [echo] file2
相关问题