蒂亚戈尔席尔瓦,
使用 xmlproperty 和 javascript 的组合,我能够拆分 projects.xml 文件的多元素(相同节点)并在 ant 中使用项目节点数据字段。所有操作都在单个 ant 文件中执行,一次执行。
上例中的示例数据 projects.xml 文件:
<projects>
<project>
<name>Project 1</name>
<url>//someurl1</url>
<package>project1</package>
</project>
<project>
<name>Project 2</name>
<url>//someurl2</url>
<package>project2</package>
</project>
</projects>
必须转换为以下格式的 projects.xml 文件:
<myProjects>
<!-- name;url;package -->
<myProject>Project 1;//someurl1;project1</myProject>
<myProject>Project 2;//someurl2;project2</myProject>
</myProjects>
在这里,先前的节点元素被转换为一个字符串,其中的字段由“;”分隔 特点。
我已将元素“project”重命名为“myProject”以避免混淆,因为我将在 javascript 中使用 ant“project”对象。
一旦我们将可重复的 xml 节点展平为字符串,我们就可以使用以下 ant 脚本来遍历元素,读取展平的属性并根据需要使用它们。
<project name="testProjects" default="ProcessProjects" basedir=".">
<target name="init" >
<!-- From: http://sourceforge.net/projects/ant-contrib/files/ant-contrib/
download the jar and put it a created "lib" folder of your project, where ant file is.
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath><pathelement location="./lib/ant-contrib-1.0b3.jar"/></classpath>
</taskdef>
<xmlproperty file="./projects.xml" keepRoot="false"/>
<!-- Properties will be used to store the values of: name;url;package -->
<property name="name" value=""/>
<property name="url" value=""/>
<property name="package" value=""/>
</target>
<target name="ProcessProjects" depends="init">
<echo>Process Projects</echo>
<foreach list="${myProject}" target="processLoopStep" param="var1"/>
</target>
<target name="processLoopStep" >
<script language="javascript"> <![CDATA[
var var1 = project.getProperty("var1");
var parts = var1.split(";");
project.setProperty("name",parts[0]);
project.setProperty("url",parts[1]);
project.setProperty("package",parts[2]);
]]>
</script>
<!-- Do what you like with the values of the <project> element -->
<echo message="myProject name = ${name} url = ${url} package = ${package}"/>
</target>
</project>
中的文件: - C:\temp\lib\ProcessProjects.xml - 构建 - C:\temp\lib\projects.xml - 数据 - C:\temp\lib\ant-contrib-1.0b3.jar -库罐
输出:
C:\temp>ant -f ProcessProjects.xml
Buildfile: C:\temp\ProcessProjects.xml
init:
ProcessProjects:
[echo] Process Projects
processLoopStep:
[echo] myProject name = Project 1 url = //someurl1 package = project1
processLoopStep:
[echo] myProject name = Project 2 url = //someurl2 package = project2
BUILD SUCCESSFUL
Total time: 0 seconds
问候, 尤雷克