Basic MSBuild script

7 commentsWritten on July 4th, 2008 by
Categories: MSBuild

I spent some time with MSBuild this week... I actually like it (yea that's right, i said it!), but it did take me some time to figure out how to write a basic build script from scratch that can be used to build a solution and run the tests. Obviously, i wanted the script to assume as little as possible, so i could pretty much drop it any solution folder and use it as is. The only thing that is hardcoded is the path the nunit console runner but other than that, this should provide a good start for pretty much any project.

<?xml version="1.0" encoding="utf-8" ?>

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

 

  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

 

  <PropertyGroup>

    <Configuration>Debug</Configuration>

    <NUnitRunner>C:\Program Files\NUnit 2.4.7\bin\nunit-console.exe</NUnitRunner>

  </PropertyGroup>

 

  <ItemGroup>

    <ProjectsToBuild Include="**\*.csproj" />

  </ItemGroup>

 

  <Target Name="BuildAll" DependsOnTargets="Clean;CoreBuild;RunTests"/>

 

  <Target Name="CoreBuild">

    <MSBuild Projects="@(ProjectsToBuild)" ContinueOnError="false" Properties="Configuration=$(Configuration)">

      <Output ItemName="BuildOutput" TaskParameter="TargetOutputs"/>

    </MSBuild>

  </Target>

 

  <Target Name="Clean">

    <MSBuild Projects="@(ProjectsToBuild)" ContinueOnError="false" Targets="Clean"

            Properties="Configuration=$(Configuration)" />

  </Target>

 

  <Target Name="RunTests" DependsOnTargets="CoreBuild">

    <CreateItem Include="**\Bin\Debug\*Tests*.dll" >

      <Output TaskParameter="Include" ItemName="TestAssemblies" />

    </CreateItem>

 

    <NUnit ToolPath="$(NUnitPath)" Assemblies="@(TestAssemblies)" DisableShadowCopy="true" />

  </Target>

 

</Project>

it uses the MSBuild Community tasks btw... Like i said, this is a very basic script. It only builds your solution and it runs all of your tests. That's it. No packaging of the output, no code coverage (if you're really doing TDD, code coverage is useless anyway), no whatever else you can think of. But you can easily add all of that stuff of course :)

Now watch me get flamed for not using nant :P

  • Stefan

    shtt… be very silent about your likings or you’ll become an mvp or .. a regional director !

  • Shyamala

    Very good Script for starters. Helped me a lot. Thank you.

  • Pingback: The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » The CI Build should be a given

  • Zak

    Where is DefaultTargets=”Build”. The target does not exist in your script. Should that have been Buildall

  • http://davybrion.com Davy Brion

    @Zak

    indeed it should have been :)

  • http://www.fuzzelogicSolutions.com/wordpress Zak

    @Davy : Thanks. I thought I missed something.

  • Anne-Marie

    I am very new to writing scripts to build/run .net projects. Could you tell me what command I need to give (and to what path I need to point) in order to run this sample script?

    Thanks!