2008-08-28 62 views
6

我是NAnt的新手,但对Ant和CruiseControl有一些经验。NAnt和双平台构建 - 在Windows和Mono/Linux上构建的最佳方式

我想要做的是让我的SVN项目包括所需的所有工具(如NUnit和Mocks等),以便我可以检查到新机器上并构建。这个策略是由J.P Boodhoo提出的here.

到目前为止,如果我只想运行在Windows上,但我希望能够在Linux上运行并且可以在Mono上构建/测试/运行。我不需要SVN项目外部的依赖关系。我不介意在项目中有两套工具,但只需要一个NAnt构建文件

这一定是可能的 - 但是如何?什么是技巧/'年轻球员的陷阱'

回答

8

这不应该是一个特别困难的练习。在我的一个项目中,我们做了一些相当类似的工作,因为它的一半运行在使用Ant的Java上运行相关目标,另一半则是用于UI的.Net(C#)。这些项目运行在Windows机器上进行开发,但服务器(Java)运行linux,但在UAT环境(linux)中,我们需要运行nunits(集成测试)。真正的技巧(不是一个很难的技巧)背后有一个NAnt构建文件,可以在这两种环境中运行,这似乎是你在这里尝试做的同样的事情。

当然你意识到你需要先在Mono安装楠:

$ 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 for MSBuild,svn for tool等)

欲了解Mono上Nunit等的更多帮助,请查看this fantastic post

希望帮助,

干杯,

罗布摹

1

值得注意的是,很多像单南特运行“开箱即用”的工具,即

mono nant.exe 

作品

2

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

对于其他一些很好的例子,一定要浏览NUnit源代码。只要我能确定它正在Mono上进行构建和测试,我就会与Charlie密切合作。他尽可能地尝试跑步。

0

我使用以下模板。它允许在任何平台上简单构建(在Win上为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 "[email protected]" 

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>