<p>I want to load a different properties file based upon one variable.</p>

<p>Basically, if doing a dev build use this properties file, if doing a test build use this other properties file, and if doing a production build use yet a third properties file.</p>

[{'answer_id': 77099, 'author': 'Andy Whitfield', 'author_id': 4805, 'author_profile': 'https://Stackoverflow.com/users/4805', 'pm_score': 0, 'selected': False, 'text': '<p>The way I\'ve done this kind of thing is to include seperate build files depending on the type of build using the <a href="http://nant.sourceforge.net/release/latest/help/tasks/nant.html" rel="nofollow noreferrer">nant task</a>. A possible alternative might be to use the <a href="http://nantcontrib.sourceforge.net/release/latest/help/tasks/iniread.html" rel="nofollow noreferrer">iniread task in nantcontrib</a>.</p>\n'}, {'answer_id': 78583, 'author': 'Tim', 'author_id': 10363, 'author_profile': 'https://Stackoverflow.com/users/10363', 'pm_score': 3, 'selected': False, 'text': '<p>You can use the <a href="http://nant.sourceforge.net/release/latest/help/tasks/include.html" rel="nofollow noreferrer"><code>include</code></a> task to include another build file (containing your properties) within the main build file. The <code>if</code> attribute of the <code>include</code> task can test against a variable to determine whether the build file should be included:</p>\n\n<pre><code>&lt;include buildfile="devPropertyFile.build" if="${buildEnvironment == \'DEV\'}"/&gt;\n&lt;include buildfile="testPropertyFile.build" if="${buildEnvironment == \'TEST\'}"/&gt;\n&lt;include buildfile="prodPropertyFile.build" if="${buildEnvironment == \'PROD\'}"/&gt;\n</code></pre>\n'}, {'answer_id': 87752, 'author': 'scott.caligan', 'author_id': 14814, 'author_profile': 'https://Stackoverflow.com/users/14814', 'pm_score': 6, 'selected': True, 'text': '<p><strong>Step 1</strong>: Define a property in your NAnt script to track the environment you\'re building for (local, test, production, etc.).</p>\n\n<pre><code>&lt;property name="environment" value="local" /&gt;\n</code></pre>\n\n<p><strong>Step 2</strong>: If you don\'t already have a configuration or initialization target that all targets depends on, then create a configuration target, and make sure your other targets depend on it.</p>\n\n<pre><code>&lt;target name="config"&gt;\n    &lt;!-- configuration logic goes here --&gt;\n&lt;/target&gt;\n\n&lt;target name="buildmyproject" depends="config"&gt;\n    &lt;!-- this target builds your project, but runs the config target first --&gt;\n&lt;/target&gt;\n</code></pre>\n\n<p><strong>Step 3</strong>: Update your configuration target to pull in an appropriate properties file based on the environment property.</p>\n\n<pre><code>&lt;target name="config"&gt;\n    &lt;property name="configFile" value="${environment}.config.xml" /&gt;\n    &lt;if test="${file::exists(configFile)}"&gt;\n        &lt;echo message="Loading ${configFile}..." /&gt;\n        &lt;include buildfile="${configFile}" /&gt;\n    &lt;/if&gt;\n    &lt;if test="${not file::exists(configFile) and environment != \'local\'}"&gt;\n        &lt;fail message="Configuration file \'${configFile}\' could not be found." /&gt;\n    &lt;/if&gt;\n&lt;/target&gt;\n</code></pre>\n\n<p>Note, I like to allow team members to define their own local.config.xml files that don\'t get committed to source control.  This provides a nice place to store local connection strings or other local environment settings.</p>\n\n<p><strong>Step 4</strong>: Set the environment property when you invoke NAnt, e.g.:</p>\n\n<ul>\n<li>nant -D:environment=dev</li>\n<li>nant -D:environment=test</li>\n<li>nant -D:environment=production</li>\n</ul>\n'}, {'answer_id': 180438, 'author': 'Ryan Taylor', 'author_id': 19977, 'author_profile': 'https://Stackoverflow.com/users/19977', 'pm_score': 3, 'selected': False, 'text': '<p>I had a similar problem which the answer from scott.caligan partially solved, however I wanted people to be able to set the environment and load the appropriate properties file just by specifying a target like so:</p>\n\n<ul>\n<li>nant dev</li>\n<li>nant test</li>\n<li>nant stage</li>\n</ul>\n\n<p>You can do this by adding a target that sets the environment variable. For instance:</p>\n\n<pre><code>&lt;target name="dev"&gt;\n  &lt;property name="environment" value="dev"/&gt;\n  &lt;call target="importProperties" cascade="false"/&gt;\n&lt;/target&gt;\n\n&lt;target name="test"&gt;\n  &lt;property name="environment" value="test"/&gt;\n  &lt;call target="importProperties" cascade="false"/&gt;\n&lt;/target&gt;\n\n&lt;target name="stage"&gt;\n  &lt;property name="environment" value="stage"/&gt;\n  &lt;call target="importProperties" cascade="false"/&gt;\n&lt;/target&gt;\n\n&lt;target name="importProperties"&gt;\n  &lt;property name="propertiesFile" value="properties.${environment}.build"/&gt;\n  &lt;if test="${file::exists(propertiesFile)}"&gt;\n    &lt;include buildfile="${propertiesFile}"/&gt;\n  &lt;/if&gt;\n  &lt;if test="${not file::exists(propertiesFile)}"&gt;\n    &lt;fail message="Properties file ${propertiesFile} could not be found."/&gt;\n  &lt;/if&gt;\n&lt;/target&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76870', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9052/']