Dot-Net

有沒有辦法在 NAnt 中動態載入屬性文件?

  • September 16, 2008

我想根據一個變數載入不同的屬性文件。

基本上,如果進行開發建構使用這個屬性文件,如果進行測試建構使用這個其他屬性文件,如果進行生產建構使用第三個屬性文件。

第 1 步:在您的 NAnt 腳本中定義一個屬性以跟踪您正在建構的環境(本地、測試、生產等)。

<property name="environment" value="local" />

第 2 步:如果您還沒有所有目標都依賴的配置或初始化目標,則創建一個配置目標,並確保您的其他目標都依賴它。

<target name="config">
   <!-- configuration logic goes here -->
</target>

<target name="buildmyproject" depends="config">
   <!-- this target builds your project, but runs the config target first -->
</target>

第 3 步:更新您的配置目標以根據環境屬性拉入適當的屬性文件。

<target name="config">
   <property name="configFile" value="${environment}.config.xml" />
   <if test="${file::exists(configFile)}">
       <echo message="Loading ${configFile}..." />
       <include buildfile="${configFile}" />
   </if>
   <if test="${not file::exists(configFile) and environment != 'local'}">
       <fail message="Configuration file '${configFile}' could not be found." />
   </if>
</target>

注意,我喜歡允許團隊成員定義他們自己的 local.config.xml 文件,這些文件不會送出到原始碼管理。這提供了一個儲存本地連接字元串或其他本地環境設置的好地方。

第 4 步:在呼叫 NAnt 時設置環境屬性,例如:

  • 南特-D:環境=開發
  • 南特-D:環境=測試
  • 南特-D:環境=生產

引用自:https://stackoverflow.com/questions/76870