Apache Ant 提供一個可編輯屬性檔案的選用任務。當想要對應用程式伺服器和應用程式的組態檔進行無人值守修改時,這項功能非常有用。目前,此任務維護一個可用的屬性檔案,並具備新增屬性或變更現有屬性的能力。自 Ant 1.8.0 起,原始屬性檔案的註解和配置會保留。
自 Ant 1.8.2 起,原始檔案的換行樣式也會保留,只要使用的樣式一致即可。一般而言,更新後檔案的換行會與讀取時找到的第一個換行相同。
屬性 | 說明 | 必要 |
---|---|---|
file | 要編輯的屬性檔案位置 | 是 |
comment | 檔案本身的標頭 | 否 |
jdkproperties | 使用 java.lang.Properties ,這會遺失檔案的註解和配置。自 Ant 1.8.0 起 |
否;預設為 false |
布林屬性 jdkproperties 用於復原任務的先前行為,其中屬性檔案的配置和任何註解都會遺失。
使用巢狀 <entry>
元素指定對屬性檔案本身的實際修改。
屬性 | 說明 | 必要 |
---|---|---|
key | 屬性名稱/值配對的名稱 | 是 |
value | 要設定 (=)、新增 ( +) 或減去 ( -) 的值 |
如果 operation 不是 del,則至少必須指定一個 |
default | 如果屬性檔案中尚未定義,則設定屬性的初始值。 對於類型 date,允許使用額外的關鍵字: now | |
type | 將值視為:int、 date或 string(預設) |
否;預設為 string |
operation | 下列操作之一 適用於所有資料類型
|
否;預設為 = |
pattern | 僅適用於 int 和 date 類型。如果存在,則會解析和格式化值。 | 否 |
unit | 要套用至 date +/ -操作的值單位。有效值為
+/ -操作的 date 類型。 |
否;預設為 天 |
設定屬性值時使用的規則如下所示。操作會在考慮這些規則之後發生。
下列會變更 my.properties 檔案。假設 my.properties 如下所示
# A string value akey=original value # The following is a counter, which will be incremented by 1 for # each time the build is run. anint=1
執行後,檔案會如下所示
#My properties #Wed Aug 31 13:47:19 BST 2005 # A string value akey=avalue # The following is a counter, which will be incremented by 1 for # each time the build is run. anint=2 adate=2005/08/31 13\:47 formated.int=0014 formated.date=243 13\:47
斜線符合 Properties 類別的預期。檔案會以必要時檢查並跳脫每個字元的格式儲存。
原始檔案的配置和註解會保留。新的屬性會新增到檔案的結尾。現有的屬性會在原地覆寫。
<propertyfile file="my.properties" comment="My properties"> <entry key="akey" value="avalue"/> <entry key="adate" type="date" value="now"/> <entry key="anint" type="int" default="0" operation="+"/> <entry key="formated.int" type="int" default="0013" operation="+" pattern="0000"/> <entry key="formated.date" type="date" value="now" pattern="DDD HH:mm"/> </propertyfile>
產生相對於今日的日期
<propertyfile file="my.properties" comment="My properties"> <entry key="formated.date-1" type="date" default="now" pattern="DDD" operation="-" value="1"/> <entry key="formated.tomorrow" type="date" default="now" pattern="DDD" operation="+" value="1"/> </propertyfile>
字串串接
<propertyfile file="my.properties" comment="My properties"> <entrykey="progress" default="" operation="+" value="."/> </propertyfile>
每次呼叫時,會將 .
附加到 progress
將專案版本提升到下一個次要版本(增加次要版本,並將 patch=0
)
<target name="nextMinorVersion"> <property name="header" value="##Generated file - do not modify!"/> <propertyfile file="version.properties" comment="${header}"> <entry key="product.build.major" type="int" value="3"/> <entry key="product.build.minor" type="int" operation="+"/> <entry key="product.build.patch" type="int" value="0"/> <entry key="product.build.date" type="date" value="now"/> </propertyfile> </target>
執行此目標後,版本會變更,例如從 3.2.2 變更為 3.3.0。