如何在NAnt中设置工作目录?

Mar*_*ski 6 nant build-automation build-process build visual-studio

我刚刚开始使用NAnt.我正在从一个教程中工作,只是试图设置一个目标来清理我的构建解决方案.我的Visual Studio Solution结构如下:

  • Solution Folder
    • 项目文件夹
    • 项目文件夹
    • 工具文件夹
      • NAnt文件夹

NAnt .exe文件位于Tools/NAnt文件夹中.我的.build文件也在那里.这是我的.build文件:

<?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>
Run Code Online (Sandbox Code Playgroud)

这就是我所遵循的示例的格式.如果我尝试运行此构建,则会收到一条错误,指出项目文件不存在.在clean.source目标中,如果我将workingdir属性替换为基本解决方案文件夹的硬编码路径,则脚本将编译并正确运行.显然,如果我需要将项目移动到任何地方,这对于便携性来说并不理想.

如何让NAnt查看基本工作目录?

Pet*_*old 7

我的建议是始终将构建文件放在解决方案级别.然后构建文件中的所有相对路径将等于解决方案的相对路径.


cao*_*cao 7

没有内置函数来更改当前目录,但您可以在脚本块中创建一个:

  <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>
Run Code Online (Sandbox Code Playgroud)

当然,您应该避免依赖脚本或代码中的当前目录.