CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

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.



Comments

Jason Haley said:

# September 11, 2006 9:27 PM

Jean-Francois Cantin said:

you might want to look at Jean-Paul Boodhoo blog. He has a nice little trick with xml template file that does just that. So your nant file stays totally uncontaminated of machine specifics.

http://www.jpboodhoo.com/blog/AutomatingYourBuildsWithNAntPart6.aspx

-JF

# September 12, 2006 11:28 AM

Christopher Steen said:

"Atlas" 1.0 Naming and Roadmap [Via: ScottGu ] BetaMarker.com - Early adopters welcome [Via: RoyOsherove...

# September 12, 2006 9:17 PM

Joshua Flanagan said:

For additional reference/cross-training, I've posted an equivalent MSBuild script:

http://flimflan.com/blog/MachineSpecificTasksWithMSBuild.aspx

# September 12, 2006 10:30 PM

Jeremy D. Miller said:

Josh & Jean-Francois, thanks for the tips.

# September 13, 2006 8:49 AM

paper machines said:

I'm going to capture this now .

# June 20, 2008 4:37 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

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 previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP