1 つの変数に基づいて別のプロパティ ファイルをロードしたいと考えています。
基本的に、開発ビルドを行う場合はこのプロパティ ファイルを使用し、テスト ビルドを行う場合はこの他のプロパティ ファイルを使用し、製品ビルドを行う場合はさらに 3 番目のプロパティ ファイルを使用します。
1 つの変数に基づいて別のプロパティ ファイルをロードしたいと考えています。
基本的に、開発ビルドを行う場合はこのプロパティ ファイルを使用し、テスト ビルドを行う場合はこの他のプロパティ ファイルを使用し、製品ビルドを行う場合はさらに 3 番目のプロパティ ファイルを使用します。
ステップ 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 を呼び出すときに環境プロパティを設定します。
このタスクを使用してinclude
、メイン ビルド ファイル内に別のビルド ファイル (プロパティを含む) を含めることができます。タスクのif
属性はinclude
、変数に対してテストして、ビルド ファイルを含める必要があるかどうかを判断できます。
<include buildfile="devPropertyFile.build" if="${buildEnvironment == 'DEV'}"/>
<include buildfile="testPropertyFile.build" if="${buildEnvironment == 'TEST'}"/>
<include buildfile="prodPropertyFile.build" if="${buildEnvironment == 'PROD'}"/>
scott.caligan からの回答で部分的に解決された同様の問題がありましたが、次のようにターゲットを指定するだけで、環境を設定して適切なプロパティ ファイルをロードできるようにしたかったのです。
これを行うには、環境変数を設定するターゲットを追加します。例えば:
<target name="dev">
<property name="environment" value="dev"/>
<call target="importProperties" cascade="false"/>
</target>
<target name="test">
<property name="environment" value="test"/>
<call target="importProperties" cascade="false"/>
</target>
<target name="stage">
<property name="environment" value="stage"/>
<call target="importProperties" cascade="false"/>
</target>
<target name="importProperties">
<property name="propertiesFile" value="properties.${environment}.build"/>
<if test="${file::exists(propertiesFile)}">
<include buildfile="${propertiesFile}"/>
</if>
<if test="${not file::exists(propertiesFile)}">
<fail message="Properties file ${propertiesFile} could not be found."/>
</if>
</target>
私がこの種のことをした方法は、nantタスクを使用してビルドのタイプに応じて別々のビルドファイルを含めることです。考えられる代替案は、nantcontribでinireadタスクを使用することです。