2016-03-01 35 views
1

我正在尋找一種方法來從螞蟻腳本中的文件加載屬性。具體來說,我想遍歷一系列屬性文件,並在每個循環中加載當前文件的屬性並對其進行處理。事情是這樣的:apache螞蟻可變文件類型屬性

<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中的可變任務來解決它。但是,我不知道如何在這裏應用它們,或者他們甚至在這種情況下爲解決方案做出貢獻。

+0

回答@vanje解決了我的問題。但是,由於我已經使用了ant-contrib,我發現我可以通過使用內部循環中的<來簡化代碼,它的行爲就像' '用純螞蟻。 – td2142

回答

1

在這裏,我使用AntContrib和兩個屬性文件在build.xml相同的目錄中。

p1.properties:

property1=from p1 

p2.properties:

property1=from p2 

的技巧是使用antcall裏面的for循環調用另一個目標。在被調用目標中設置的屬性不會傳播回調用方。

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

在我原來的問題我遇到了麻煩加載整個場所內的文件進行循環(從螞蟻的contrib)。在for循環內部,ant-contrib自己的var任務與純蟻的屬性任務完全相同。我所要做的就是將<property file="@{file}" prefix="fromFile"/>替換爲<var file="@{file}"/>。加載的屬性將被最新的值覆蓋,我甚至不需要前綴屬性來跟蹤我目前在哪個循環。