簡體   English   中英

apache ant可變文件類型屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM