How to set working directory to NAnt?

I'm just starting to use NAnt. I worked from a tutorial and just tried to set a goal to clear my solution during assembly. The structure of my Visual Studio Solution is as follows:

  • Solutions folder
    • Project folder
    • Project folder
    • Tool folder
      • NAnt folder

The NAnt.exe file is located in the Tools / NAnt folder. The .build file is also located here. 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 following example was formatted. If I try to run this assembly, I get an error that the project file does not exist. For the purpose of clean.source, if I replace the workdir attribute with a hard-coded path to my solution base 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?

+6
build-process build-automation visual-studio build nant
source share
6 answers

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

+7
source share

There is no built-in function to change the current directory, but you can create it in a script block:

  <target name="foo"> <echo message="Current directory set to ${directory::set-current-directory('C:')}"/> <echo message="Current directory is now ${directory::get-current-directory()}"/> </target> <script language="C#" prefix="directory"> <code><![CDATA[ [Function("set-current-directory")] public static string SetCurrentDirectory(string path) { System.IO.Directory.SetCurrentDirectory(path); return path; } ]]></code> </script> 

Of course, you should not rely on the current directory in your scripts or in your code.

+7
source share

You can try to set the basedir attribute of the node project. This may solve your problem.

 <project name="NAntTest" default="build" basedir="C:\Code\MyProject" xmlns="http://nant.sf.net/release/0.86-beta1/nant.xsd"> 
+2
source share

If you set the verbose attribute to nant exec task , then it will spit out the exact command line that it generated. Not sure what your specific problem regarding msbuild execution is. Instead, I used the nantcontrib msbuild task .

+1
source share

Now you can specify the workingdir attribute in the exec element.

According to the documentation, workingdir refers to "the directory in which the command will be executed."

0
source share

As a task instead of a function:

 <?xml version="1.0"?> <project name="test" default="build"> <script language="C#" prefix="path" > <code> <![CDATA[ [TaskName("set-current-directory")] public class SetCurrentDirectory : Task { private string _path; [TaskAttribute("path", Required=true)] public string Path { get { return _path; } set { _path = value; } } protected override void ExecuteTask() { System.IO.Directory.SetCurrentDirectory(_path);; } } ]]> </code> </script> <target name="build"> <set-current-directory path="c:\Program Files" /> <echo message="${directory::get-current-directory()}" /> </target> </project> 

Output:

 $ nant build: [echo] c:\Program Files 
0
source share

All Articles