[英]apache ant mutable file type property
我正在寻找一种从ant脚本中的文件加载属性的方法。 具体来说,我想遍历属性文件列表,并在每个循环中从当前文件加载属性并对其进行处理。 像这样:
<for param="file">
<path>
<fileset containing my properties files.../>
</path>
<sequential>
<property file="@{file}" prefix="fromFile"/>
<echo message="current file: @{file}"/>
<echo message="property1 from file: ${fromFile.property1}"/>
</sequential>
</for>
上面的代码仅导致第一个属性文件被读取,即使每个循环都遍历每个属性文件名也是如此。 我知道属性是不可变的,我可以使用ant-contrib的局部任务或可变任务来解决它。 但是,我不知道如何在这里应用它们,或者在这种情况下它们是否甚至有助于解决方案。
在这里,我在build.xml
所在的目录中使用了Antcontrib和两个属性文件。
p1.properties:
property1=from p1
p2.properties:
property1=from p2
诀窍是在for循环内使用antcall
来调用另一个目标。 在被调用目标中设置的属性不会传播回调用方。
build.xml文件:
<project name="test" default="read.property.files">
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="ant-contrib/ant-contrib-1.0b3.jar"/>
</classpath>
</taskdef>
<target name="read.property.files">
<for param="file">
<path>
<fileset dir="." includes="*.properties"/>
</path>
<sequential>
<antcall target="read.one.property.file">
<param name="propfile" value="@{file}"/>
</antcall>
</sequential>
</for>
</target>
<target name="read.one.property.file">
<property file="${propfile}" />
<echo message="current file: ${propfile}" />
<echo message="property1 from file: ${property1}"/>
</target>
</project>
输出为:
Buildfile: /home/vanje/anttest/build.xml
read.property.files:
read.one.property.file:
[echo] current file: /home/vanje/anttest/p1.properties
[echo] property1 from file: from p1
read.one.property.file:
[echo] current file: /home/vanje/anttest/p2.properties
[echo] property1 from file: from p2
BUILD SUCCESSFUL
Total time: 0 seconds
在我最初的问题中,我无法从for循环(从ant-contrib)中加载整个属性文件。 在循环内部,ant-contrib自己的var任务的工作原理与纯ant的property任务相同。 我所要做的就是用<var file="@{file}"/>
替换<property file="@{file}" prefix="fromFile"/>
<var file="@{file}"/>
。 加载的属性将被最新值覆盖,我什至不需要prefix属性来跟踪我当前所在的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.