The Inquisitive Coder – Davy Brion's Blog

Trying to walk that thin line between intelligence and ignorance

Basic MSBuild script

Posted by Davy Brion on July 4th, 2008

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

6 Responses to “Basic MSBuild script”

  1. Stefan Says:

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

  2. Shyamala Says:

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

  3. The Inquisitive Coder - Davy Brion’s Blog » Blog Archive » The CI Build should be a given Says:

    [...] what does it take then? Drop in a simple, standard build script which builds your project and runs the tests, add a build configuration and you’re done. [...]

  4. Zak Says:

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

  5. Davy Brion Says:

    @Zak

    indeed it should have been :)

  6. Zak Says:

    @Davy : Thanks. I thought I missed something.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>