2013-05-03 19 views
4

我想跟着我已经给出的一个大的蚁群构建文件,并且在这种情况下我无法理解xmlproperty的功能。 考虑这个xml文件example.xml。Ant xmlproperty任务。当有多个同名的标签时会发生什么?

<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> s,${PREFIX.main.tagList.tag.file}的值是多少?如果它是某种列表,我如何迭代<file>值?

我正在使用ant 1.6.2。

回答

9

当多个元件具有相同的名称,<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 

为了处理逗号分隔的值,可以考虑使用the <for> task来自第三方Ant-Contrib库:

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

很好的解释,谢谢。为了澄清读者的其他人,默认情况下,标签似乎在逗号分隔符上循环,这与相同的xml属性默认分隔符很好地匹配。 – 2013-05-03 15:52:25

+0

假设每个''都有一个唯一的属性值。有没有办法使用该属性值来访问属性的值,而不是使用Ant-Contrib? – Scribblemacher 2016-07-12 14:25:28

相关问题