Machine specific tasks with NAnt

I always have to look up how to do this, so I'm going to capture this now.  If you ever need to make a NAnt script branch based on the machine it's running on, here's an easy way.  First, go and get the machine name that the NAnt script is running on:

            <property name="machine" value="${environment::get-machine-name()}" />

Then use the if and call tasks to call another target named after the machine:

              <if test="${target::exists(machine)}"> 

                <call target="${machine}"/>

              </if>

Then simply create a separate task for each machine name like this:

      <target name="TEST-SERVER">

          <property name="database" value="invoice_test" />   

      </target>

 

      <target name="PRODUCTION-SERVER">

          <property name="database" value="invoice_prod" /> 

      </target>

We use it occasionally in build scripts to handle differences in developer workstations (try not to do this though) and server environments without forking the build scripts.

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Continuous Integration. Bookmark the permalink. Follow any comments here with the RSS feed for this post.