【问题标题】:NAnt and dual platform build - best way to build on Windows AND Mono/LinuxNAnt 和双平台构建 - 在 Windows 和 Mono/Linux 上构建的最佳方式
【发布时间】:2010-09-07 02:49:06
【问题描述】:

我是 NAnt 新手,但对 Ant 和 CruiseControl 有一些经验。

我想做的是让我的 SVN 项目包含所有需要的工具(如 NUnit 和 Mocks 等),这样我就可以在新机器上签出并进行构建。该策略由 J.P Boodhoo here.

概述

到目前为止,如果我只想在 Windows 上运行,那就太好了,但我也希望能够在 Linux 上签出并针对 Mono 构建/测试/运行。我不想要 SVN 项目外部的依赖项。我不介意项目中有两套工具,但只需要一个 NAnt 构建文件

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

【问题讨论】:

    标签: .net linux build-process mono nant


    【解决方案1】:

    这应该不是特别困难的练习。我们在我的一个项目中做了一些非常相似的事情,因为其中一半在 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 等的更多帮助,请查看this fantastic post

    希望对你有帮助,

    干杯,

    罗伯克

    【讨论】:

      【解决方案2】:

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

      对于其他一些好的示例,请务必浏览 NUnit 源代码。我尽可能与 Charlie 密切合作,以确保它在 Mono 上构建和测试。他也尽可能地尝试跑步。

      【讨论】:

        【解决方案3】:

        值得注意的是,像 Nant 这样的许多工具都在“开箱即用”的单声道上运行,即

        mono nant.exe
        

        作品

        【讨论】:

          【解决方案4】:

          我使用以下模板。它允许在任何平台上进行简单的构建(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 "$@"
          

          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>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-06-02
            • 2010-11-09
            • 2012-12-31
            • 1970-01-01
            • 2012-09-23
            • 2011-10-15
            • 1970-01-01
            • 2023-03-26
            相关资源
            最近更新 更多