2012-02-01 93 views
3

我想在使用propertyfile任務更新它之後讀取屬性。類似於更新屬性文件後重新加載屬性

<property file="test.properties" /> 
<echo>before :: ${modules}</echo> 

<propertyfile file="test.properties" > 
    <entry key="modules" type="string" operation="+" value="foo" /> 
</propertyfile> 

<property file="${status.path}/test.properties" /> 
<echo>after :: ${modules}</echo>. 

它似乎沒有加載第二次。但屬性文件已更新。

回答

2

由於sudocode已經提到,在覈心Ant屬性是不可變的 - 出於很好的理由。
Antelope Ant Task S中unset任務你能夠取消設置在一個文件中設置的所有屬性有一個內襯:

<unset file="test.properties"/> 

事後

<propertyfile file="test.properties" > 
    <entry key="modules" type="string" operation="+" value="foo" /> 
</propertyfile> 

會工作。

提示:該任務僅適用於普通屬性,不適用於xmlproperties。
但有一個簡單的workararound,只需使用
<echoproperties prefix="..." destfile="foo.properties"/>
事後
<unset file="foo.properties"/>
如果你不想使用羚羊爲特定的任務而已,你可以寫一個macrodef或自己的任務有類似的功能。

0

Ant屬性是不可變的 - 一旦設置,它們就會被修復。因此,重新加載屬性文件不會刷新已設置屬性的值。

0

該宏允許更改後固定一個

<macrodef name="set" > 
    <attribute name="name"/> 
    <attribute name="value"/> 
    <sequential> 
     <script language="javascript"> 
      <![CDATA[ 
      project.setProperty("@{name}", "@{value}"); 
      ]]> 
     </script> 
    </sequential> 
</macrodef> 
1

對於這種情況的屬性值,當整個屬性文件被加載兩次,我建議使用不同的前綴的第一和第二負載。第一個加載一個prefix屬性等於first。使用此前綴訪問屬性,即屬性foo將可作爲first.foo訪問。然後保存屬性文件並重新加載,但這次沒有前綴。您將在適當的地方獲得修改後的屬性。

不使用前綴,第二次加載將不會執行任何操作,因爲ant會阻止屬性重寫。其他人已經指出。

5

您可以調用與antcall任務,忽略你的主要目標運行時的性能提供了新的螞蟻運行 - 只要確保包括inheritAll="false"

<target name="main"> 
    <property file="test.properties"/> 
    <echo>before :: ${modules}</echo> 

    <propertyfile file="test.properties"> 
     <entry key="modules" type="string" operation="+" value="foo" /> 
    </propertyfile> 

    <antcall target="second-runtime" inheritAll="false"/> 
</target> 

<target name="second-runtime"> 
    <property file="${status.path}/test.properties" /> 
    <echo>after :: ${modules}</echo> 
</target> 

antcall refrence

0

你可以創建一個新的屬性文件並將該屬性保存在新文件中。

在下一行提供該文件的引用。

完成:)