How to Set the Working Directory in NAnt?

Mark Struzinski picture Mark Struzinski · Apr 16, 2009 · Viewed 8.6k times · Source

I am just getting started using NAnt. I was working from a tutorial, and just trying to set a target to clean my solution on build. My Visual Studio Solution structure is as follows:

  • Solution Folder
    • Project Folder
    • Project Folder
    • Tools Folder
      • NAnt Folder

The NAnt .exe file resides in the Tools/NAnt folder. My .build file is also in there. Here is my .build file:

<?xml version="1.0" encoding="utf-8" ?>
<project name="NAntTest" default="build" xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd">
  <property name="solution.file.name" value="NAntTest.sln" />
  <property name="project.config" value="debug" />

  <target name="build" depends="clean.source" />

  <target name="clean.source">
    <exec program="${framework::get-framework-directory(framework::get-target-framework())}\msbuild.exe"
          commandline="${solution.file.name} /t:Clean /p:Configuration=${project.config} /v:q" 
          workingdir="."/>
  </target>

</project>

This is how the example I am following was formatted. If I try to run this build, I get an error stating that the project file does not exist. In the clean.source target, if I replace the workingdir attribute with a hard coded path to my base solution folder, the script compiles and runs correctly. Obviously, this is not ideal for portability if I need to move the project anywhere.

How do I get NAnt to see the base working directory?

Answer

Peter Lillevold picture Peter Lillevold · Apr 16, 2009

My recommendation is to always place the build file at solution level. Then all relative paths in the build file will be equal to that of the solution.