NAnt和双平台构建 - 在Windows和Mono/Linux上构建的最佳方式

6

我对NAnt比较陌生,但熟悉Ant和CruiseControl。

我的目标是使我的SVN项目包含所有所需的工具(像NUnit和Mocks等),这样我就可以在全新的机器上检出并进行构建。J.P Boodhoo在这里概述了此策略。

如果我只想在Windows上运行,那么一切都很顺利,但我也想能够在Linux上检出并使用Mono进行构建/测试/运行。我不想依赖SVN项目之外的任何内容。我不介意在项目中有两组工具,但只想要一个NAnt构建文件。

这一定是可能的-但如何实现?有什么技巧/需要注意的问题吗?

4个回答

8
这不应该是一个特别困难的练习。我的一个项目中有一半是在Java上运行相关目标,使用Ant来运行,而另一半则是UI方面的.Net(C#)。项目在开发时在Windows机器上运行,但服务器(Java)在Linux上运行,在UAT环境(Linux)中需要运行nunits(集成测试)。真正的技巧(并不是很难的技巧)在于拥有一个能够在两个环境中运行的NAnt构建文件,这似乎是您在这里试图做的相同的事情。
当然,您需要先在Mono上安装NAnt:
$ export MONO_NO_UNLOAD=1
$ make clean
$ make
$ mono bin/NAnt.exe clean build

然后,您的构建文件需要以这样的方式编写,即分离关注点。例如,为Windows编写的构建文件的某些部分在Linux中无法工作。因此,您确实只需要将其分成构建文件中的特定目标。之后,您可以通过多种方式从命令行运行特定目标。例如:

<project name="DualBuild">
  <property name="windowsDotNetPath" value="C:\WINDOWS\Microsoft.NET\Framework\v3.5" />
  <property name="windowsSolutionPath" value="D:\WorkingDirectory\branches\1234\source" />
  <property name="windowsNUnitPath" value="C:\Program Files\NUnit-Net-2.0 2.2.8\bin" />
  <property name="monoPath" value="You get the idea..." />

  <target name="BuildAndTestOnWindows" depends="WinUpdateRevision, WinBuild, WinTest" />
  <target name="BuildAndTestOnLinux" depends="MonoUpdateRevision, MonoBuild, MonoTest" />

  <target name="WinUpdateRevision">
    <delete file="${windowsSolutionPath}\Properties\AssemblyInfo.cs" />
    <exec program="subwcrev.exe" basedir="C:\Program Files\TortoiseSVN\bin\"
          workingdir="${windowsSolutionPath}\Properties"
          commandline="${windowsSolutionPath} .\AssemblyInfoTemplate.cs
                       .\AssemblyInfo.cs" />
    <delete file="${windowsSolutionPath}\Properties\AssemblyInfo.cs" />
    <exec program="subwcrev.exe" basedir="C:\Program Files\TortoiseSVN\bin\"
          workingdir="${windowsSolutionPath}\Properties"
          commandline="${windowsSolutionPath} .\AssemblyInfoTemplate.cs 
                       .\AssemblyInfo.cs" />
  </target>

  <target name="WinBuild">
    <exec program="msbuild.exe"
          basedir="${windowsDotNetPath}"
          workingdir="${windowsSolutionPath}"
          commandline="MySolution.sln /logger:ThoughtWorks.CruiseControl.MsBuild.XmlLogger,
                       ThoughtWorks.CruiseControl.MsBuild.dll;msbuild-output.xml 
                       /nologo /verbosity:normal /noconsolelogger 
                       /p:Configuration=Debug /target:Rebuild" />
  </target>

  <target name="WinTest">
    <exec program="NCover.Console.exe"
          basedir="C:\Program Files\NCover"
          workingdir="${windowsSolutionPath}">
      <arg value="//x &quot;ClientCoverage.xml&quot;" />
      <arg value="&quot;C:\Program Files\NUnit-Net-2.0 2.2.8\bin
                       \nunit-console.exe&quot; 
                       MySolution.nunit /xml=nunit-output.xml /nologo" />
    </exec>
  </target>

  <target name="MonoUpdateRevision">
    You get the idea...
  </target>


  <target name="MonoBuild">
    You get the idea...
  </target>

  <target name="MonoTest">
    You get the idea...
  </target>

</project>

为了简洁起见,我省略了双方。有趣的是,您可以在两个环境中使用NUnit和NAnt,并且从依赖性的角度来看,这使得事情变得非常容易。对于每个可执行文件,您都可以将其替换为在该环境中工作的其他文件,例如(xBuild用于MSBuild,svn用于tortoise等)。
如需更多关于Mono上的Nunit等帮助,请查看这篇精彩的文章
希望能帮到您,
祝好,
Rob G

2

@Rob G - 嘿!那是我的帖子!;)

如果你想要更多好的例子,一定要去浏览NUnit源代码。我尽量和Charlie密切合作,确保它在Mono上构建和测试。他也会在有机会时进行运行。


1
值得注意的是,许多工具(如Nant)可以直接在Mono上运行。
mono nant.exe

工作正常


0
我使用以下模板。它允许在任何平台上进行简单构建(在 Windows 上使用 build,在 Linux 上使用 ./build.sh),并最小化构建脚本中的重复内容。

NAnt可执行文件与项目一起存储在tools\nant中。

构建配置文件决定要使用哪个构建工具,可以是MSBuild或xbuild(在这种情况下,对于Windows,我需要VS2015 MSBuild版本,请根据需要更改路径)。

build-csproj构建目标可用于在解决方案中有多个项目的情况下进行重用。

test-project目标需要根据您的需求进行扩展。

build.bat

@tools\nant\nant.exe %*

build.sh

#!/bin/sh

/usr/bin/cli tools/nant/NAnt.exe "$@"

default.build

<?xml version="1.0"?>
<project name="MyProject" default="all">

  <if test="${not property::exists('configuration')}">
    <property name="configuration" value="release" readonly="true" />
  </if>

  <if test="${platform::is-windows()}">
    <property name="BuildTool" value="C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" readonly="true"/>
  </if>
  <if test="${platform::is-unix()}">
    <property name="BuildTool" value="xbuild" readonly="true"/>
  </if>

  <property name="TestTool" value="tools/mytesttool.exe"/>

  <target name="all" depends="myproject myprojectlib" />

  <target name="build-csproj" description="Build a given csproj">
    <!-- Must not be called standalone as it requires some properties set. -->
    <exec program="${BuildTool}">
      <arg path="src/${ProjectName}/${ProjectName}.csproj" />
      <arg line="/property:Configuration=${configuration}" />
      <arg value="/target:Rebuild" />
      <arg value="/verbosity:normal" />
      <arg value="/nologo" />
    </exec>
  </target>

  <target name="test-project">
    <!-- Must not be called standalone as it requires some properties set. -->
    <exec program="${TestTool}">
      <arg path="my/${ProjectName}/tests/path/for/tool" />
      <arg value="/aproperty=value" />
    </exec>
  </target>

  <target name="myproject" description="Build the project">
    <property name="ProjectName" value="MyProject"/>
    <call target="build-csproj" />
    <call target="test-project" />
  </target>

  <target name="myprojectlib" description="Build the project's library dll">
    <property name="ProjectName" value="MyProjectLib"/>
    <call target="build-csproj" />
    <call target="test-project" />
  </target>

</project>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接