简体   繁体   English

apache ant可变文件类型属性

[英]apache ant mutable file type property

I'm looking for a way to load properties from a file in ant script. 我正在寻找一种从ant脚本中的文件加载属性的方法。 Specifically, I want to loop through a list of properties files, and on each loop load the properties from the current file and do something with it. 具体来说,我想遍历属性文件列表,并在每个循环中从当前文件加载属性并对其进行处理。 Something like this: 像这样:

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

The code above results in only the first properties file from being read, even though each loop does go through each properties file name. 上面的代码仅导致第一个属性文件被读取,即使每个循环都遍历每个属性文件名也是如此。 I know property is immutable, and I can get around it by using local task or variable task from ant-contrib. 我知道属性是不可变的,我可以使用ant-contrib的局部任务或可变任务来解决它。 However, I don't know how to apply them here, or if they even contribute to a solution in this case. 但是,我不知道如何在这里应用它们,或者在这种情况下它们是否甚至有助于解决方案。

Here I used Antcontrib and two property files in the same directory as the build.xml . 在这里,我在build.xml所在的目录中使用了Antcontrib和两个属性文件。

p1.properties: p1.properties:

property1=from p1

p2.properties: p2.properties:

property1=from p2

The trick is to use antcall inside the for loop to call another target. 诀窍是在for循环内使用antcall来调用另一个目标。 Properties set in the called target at not propagated back to the caller. 在被调用目标中设置的属性不会传播回调用方。

build.xml: 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>

The output is: 输出为:

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

In my original question I had trouble loading entire properties file from within for loop (from ant-contrib). 在我最初的问题中,我无法从for循环(从ant-contrib)中加载整个属性文件。 Turns out inside for loops, ant-contrib's own var task works just the same as property task from pure ant. 在循环内部,ant-contrib自己的var任务的工作原理与纯ant的property任务相同。 All I have to do is replace <property file="@{file}" prefix="fromFile"/> with <var file="@{file}"/> . 我所要做的就是用<var file="@{file}"/>替换<property file="@{file}" prefix="fromFile"/> <var file="@{file}"/> The properties loaded will be overwritten with the latest values and I don't even need prefix attribute to keep track of which loop I'm currently on. 加载的属性将被最新值覆盖,我什至不需要prefix属性来跟踪我当前所在的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM