Notes
Slide Show
Outline
1
MSBuild,
changing the way you compile for ever
  • Barry Dorrans
  • barry.dorrans@charteris.com
2
MSBuild,
changing the way you compile for ever
  • History
  • What is MSBuild?
  • Editing the project
  • Customising your build
  • Custom tasks
  • UK SDC build tasks
  • Targeting .NET v1
  • What next?
  • MSBuild Resources
3
If you build it they will come
  • In the beginning was make files
  • And make begat nmake, gmake, pmake and others
  • And there was wailing and gnashing of teeth
  • And UIs arrived and covered the shame that was make files with prettiness
  • And Java and XML arrived and begat ant
  • And heresy was spawned with .net and nant
  • And lo, the word of Bill came from on high and MSBuild came out of the darkness.
4
What is MSBuild?
  • The underlying build engine in Visual Studio 2005
  • Open and published XML based build description format
  • Customisable
  • Extensible with tasks and loggers
  • Distributed as part of the framework
  • 30% of DivDev (Visual Studio and Framework) code now built using msbuild and teambuild
5
The MSBuild & Team Build Architecture


6
Typical Build Flow
7
A sample project file
  • <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  •    <PropertyGroup>
  •       <AppName>MsBuildDemo</AppName>
  •       <DebugSymbols>true</DebugSymbols>
  •       <OutputAssembly>$(AppName).exe</OutputAssembly>
  •    </PropertyGroup>
  •    <ItemGroup>
  •       <Compile Include="Hello.cs" />
  •       <Compile Include="Program.cs" />
  •    </ItemGroup>
  •    <Target Name="Build">
  •       <Message Text="Executing Build Target for App $(AppName)" />
  •       <Csc Sources="@(Compile)" EmitDebugInformation="$(DebugSymbols)"
  •    OutputAssembly="$(OutputAssembly)"/>
  •    </Target>
  •    <Import Project="Microsoft.CSharp.targets" />
  • </Project>
8
Typical Build Output
9
Targets, Tasks and Loggers
  • A target is a group of tasks executed in order
  • A task is a unit of executable code which performs atomic build operations
  • A logger is a class which logs output from tasks and the build engine
  • All are customisable


10
Editing the project
  • MSBuild projects are XML files
  • Editing within the IDE gives
    • Intellisense
    • Colour Coding
  • In Solution Explorer:

    Right click project, choose
    “Unload Project”
    Right click project, choose
    “Edit Project”
11
Customising your build
  • Basic customisation
    • BeforeBuild and AfterBuild targets
    • Verbosity
  • Properties and Meta Data
  • Standard Tasks
  • Including other projects
  • Conditions
  • Custom Build Actions and Types
  • Target Dependencies



12
BeforeBuild / AfterBuild
  • Standard targets
  • Can be modified using the VS UI
    (Project, properties, build events)
    This only allows running of batch files
  • By default AfterBuild only fires on a successful build
  • Shared between debug, release and custom targets.
13
Verbosity
  • Quiet
  • Minimal
  • Normal
  • Detailed
  • Diagnostic
  • Can be set in IDE
    Tools / Options /
    Projects & Solutions / Build and Run
  • msbuild /verbosity:detailed


14
Properties and Meta Data
  • $(name)
  • Property Name/Value Pair
  • <PropertyGroup>
      <BuildDir>Build</BuildDir> </PropertyGroup>
  • Reserved values
    e.g. $(MSBuildProjectName)
  • Environment variables
    $(username)
15
Properties and Meta Data
  • <Target Name="Compile" DependsOnTargets="Resources">
  •   <Csc Sources="@(CSFile)" TargetType="library" Resources="@(CompiledResources)" EmitDebugInformation="$(includeDebugInformation)" References="@(Reference)" DebugType="$(debuggingType)" OutputAssembly="$(builtdir)\$(MSBuildProjectName).dll" >
  •     <Output TaskParameter="OutputAssembly" ItemName="FinalAssemblyName" />
  •     <Output TaskParameter="BuildSucceeded" PropertyName="BuildWorked" />
  •   </Csc>
  • </Target>
16
Standard Tasks
  • Basic tasks
    http://msdn2.microsoft.com/en-us/library/7z253716(en-US,VS.80).aspx
  • Includes
    • File copy, delete
    • Message
    • FindUnderPath
    • MakeDir
    • ReadLinesFromFile
    • MSBuild
    • Exec
    • OnError

17
Including other projects
  • <Import Project="external.proj">
  • Consider setting properties for builds in a separate script
  • Consider common build tasks in a separate script


18
Conditions
  • Each task and target has an optional Condition parameter
  • Boolean used to decide if task should be executed
  • <Copy Condition="!Exists('$(builtdir)')" …
19
ContinueOnError
  • Each task has an optional ContinueOnError parameter
  • Boolean used to decide if build continues if the task fails
  • Defaults to false
20
Custom Build Actions
  • Create custom target file
  • Add the following tags
    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <AvailableItemName Include="MyBuildAction" />  
      </ItemGroup>
    </Project>
  • c:\program files\msbuild\...
  • In your project file import the target
    <Import Project="$(MSBuildExtensionsPath)\MyFiles\custom.targets"/>
21
Custom Build Actions
  • From




    to


22
Custom Build Types
  • Configuration Manager


23
Target Dependencies
  • <Target Name="DailyBuild" DependsOnTargets="GetLatest;MakeClean">
  • No practical limit on dependencies
  • Nested dependencies
  • Runs in the order specified
24
Custom Tasks
  • Managed type that implements Microsoft.Build.Framework.ITask or
    uses Microsoft.Build.Utilities.Task as a base.


  • using System;
  • using Microsoft.Build.Framework;
  • using Microsoft.Build.Utilities;
  • namespace idunno.Tasks
  • {
  •     public class Hello : Task
  •     {
  •         public override bool Execute()
  •         {
  •             Log.LogMessage("Pigeons go coo");
  •             return true;
  •         }
  •     }
  • }
25
Adding the task to your project
  • Copy the task into c:\program files\msbuild
  • Add a <using> tag to your build script
    <UsingTask TaskName= "$(MSBuildExtensionsPath)\idunno.Tasks.Hello" AssemblyName="idunno.Tasks.dll"/>
  • And call in your build script
    <idunno.Tasks.Hello />
26
Input and output
  • Class properties
  • [Required]
  • [Output]
  • Log.LogMessage
    Log.LogWarning
    Log.LogError
27
Debugging custom tasks
  • Create a test project to run your task in
  • Edit the .csproj/.vbproj file and add in the necessary MSBuild XML to make use of your task.
    <UsingTask AssemblyFile="D:\tasks\assemblyinfotask.dll" TaskName="AssemblyInfo"/>
      <Target Name="BeforeBuild" Outputs="@(AssemblyInfo)">
      <AssemblyInfo AssemblyInfoFile="@(AssemblyInfo)"/>
    </Target>
  • Close the test project
  • Open your custom task project
  • Go to Project > projectname Properties… > Debug
  • Change the start action to “Start external program” and put in the full path to MSBuild as the program.
  • Change the “Command line arguments” to the full path to your test project
  • Set breakpoint on execute method
28
UK SDC Tasks
  • Tasks (and source) for common actions
  • Used to build and deploy large SDC projects
  • One project has over .75Mb of build scripts pushing to a 40 rig system, building, then releasing the install over AD, SQL, IIS and BizTalk.
29
Targeting .NET v1
  • Official support may be coming
  • Current “hacks” miss out features like COM references and Web References
  • Currently gauging interest – so express interest!
    http://blogs.msdn.com/clichten/archive/2005/10/05/477506.aspx
  • Craig Lichtenstein clichten@microsoft.com
30
Unofficially Targeting .NET v1
  • Must upgrade project file to 2005
  • MSBuild Toolkit
    http://weblogs.asp.net/rmclaws/archive/2005/06/04/410360.aspx
  • Adds easy administration of imported targets
  • Adds v1.0 and v1.1 targets
  • Simple Custom Targets File
    http://blogs.msdn.com/jomo_fisher/archive/2005/04/22/410903.aspx
31
MSBuild Parallelisation
  • Parallelisation upcoming
    • Build lab support
      Multiple projects are built concurrently
    • Parallel VS compiling
      independent projects are built concurrently
    • Multiple file compilation
      files that need to be operated on by a single tool one-by-one
  • New parallelisation “constructs” in the file format that will allow indication what can and cannot be parallelized.
  • Feedback wanted! msbuild@microsoft.com
32
MS Build Resources
  • MSDN Help
    http://msdn2.microsoft.com/en-us/library/0k6kkbsd
  • MSBuild Blog
    http://blogs.msdn.com/msbuild/
  • MSBuild Wiki
    http://channel9.msdn.com/wiki/default.aspx/MSBuild.HomePage
  • SDC Tasks
    http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=2CB20E79-D706-4706-9EA0-26188257EE7D