我正在尝试遵循已提供的大型 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>
s,${PREFIX.main.tagList.tag.file}
的值是多少?在这种情况下?如果它是某种列表,我如何遍历 <file>
值(value)观?
我使用的是 ant 1.6.2。
最佳答案
当多个元素同名时,<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
关于xml - Ant xmlproperty 任务。当有多个同名标签时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16356605/