也许在使用 flaka 时已经使用:
<!-- Activate flaka for all ant tasks -->
<fl:install-property-handler/>
结合:
#{ x * y}
会以某种方式为你工作,没有测试它,因为我的机器上没有安装 antcontrib。
属性处理程序允许在所有 ant 任务中使用 EL 表达式。
这是一个给定文件 foo.xml 的小例子,需要xmltask和flaka:
<whatever>
<sprite name="timer" path="img/folder1/img1.jpg" pos-x="25" pos-y="4" xref=""/>
<sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
<control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
</whatever>
就地编辑 foo.xml:
<project xmlns:fl="antlib:it.haefelinger.flaka">
<!-- Activate flaka for all ant tasks -->
<fl:install-property-handler/>
<!-- Import XMLTask -->
<taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
<!-- get a list with all pos-x attribute values -->
<xmltask source="foo.xml">
<copy path="//whatever/*/@pos-x" append="true" propertySeparator="," property="posxlist"/>
</xmltask>
<echo>$${posxlist} => ${posxlist}</echo>
<fl:let>counter ::= 1</fl:let>
<!-- for loop with xmltask editing foo.xml in place => source = dest -->
<fl:for var="posx" in="split('${posxlist}', ',')">
<xmltask source="foo.xml" dest="foo.xml" report="true">
<!-- i.e. multiplicating value * 3 -->
<attr path="//whatever/*[${counter}]" attr="pos-x" value="#{posx * 3}"/>
</xmltask>
<fl:let>counter ::= '${counter}' + 1</fl:let>
</fl:for>
</project>
输出 :
[xmltask] Cannot append values to properties
[xmltask] Cannot append values to properties
[xmltask] Cannot append values to properties
[echo] ${posxlist} => 25,26,27
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="26" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="27" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
[xmltask] Document -->
[xmltask] <whatever>
[xmltask] <sprite name="timer" path="img/folder1/img1.jpg" pos-x="75" pos-y="4" xref=""/>
[xmltask] <sprite name="timer1" offset-x="100" offset-y="10" path="img/folder1/img2.jpg" pos-x="78" pos-y="4" xref=""/>
[xmltask] <control name="timer2" offset-y="10" path="img/folder1/img2.jpg" pos-x="81" pos-y="4" size="100" xref=""/>
[xmltask] </whatever>
[xmltask] Document <--
BUILD SUCCESSFUL
Total time: 826 milliseconds
警告“无法将值附加到属性”来自 com.oopsconsultancy.xmltask.CopyAction 第 80 行,以强调 ant 中的属性是不可变的,可以安全地忽略 - 甚至更好地从源中删除它并重建 xmltask.jar