MSBuild: The New Build System for Visual Studio 2005 and Longhorn

MSBuild is the new build system in Visual Studio 2005. It has been built from the ground up in managed code, with scalability, performance and extensibility as core goals.

When designing MSBuild, the development team had several different customer audiences that they kept in mind:

  • The Developer - someone who writes and compiles code regularly;
  • The Build Developer - someone who implements the processes for the build environment;
  • The Build Lab Tech - the person responsible for kicking off and managing the build process, making sure that builds don't break and fixing them as necessary. Note that this person may well not have Visual Studio on their machine.
  • The Build Lab Manager - who needs to track progress of the project and the build success/fail status. This individual would also probably not be a VS user.

MSBuild is actually built into the operating system, rather than the development environment. This means you can build Visual Studio projects without VS installed; all you need is Windows (and .NET Framework 2.0 until Longhorn).

MSBuild is driven by project files, which are created either by Visual Studio (automatically) or by hand by a developer. These are XML files that describe the build process elements (targets: build, clean, rebuild etc. that each contain constituent tasks) and inputs (items and properties). The project file is ultimately fed into the MSBuild engine, which in turn then generates the output.

The XML input to MSBuild is of course strongly-typed; the schema ships with VS 2005 Beta 1 and is called msbuild.xsd: if you include the namespace definition in Visual Studio when editing with its inbuilt XML editor, you'll get full Intellisense when working with MSBuild project files.

You can write the entire build process from scratch, if you like: here's a sample build project that compiles a C# application:

 <?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="https://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
      <OutputPath>Bin\</OutputPath>
      <AssemblyName>MSBuildSample</AssemblyName>
   </PropertyGroup>
  
   <ItemGroup>
      <Compile Include="MSBuildSample.cs"></Compile>
      <Reference Include="System.dll"></Reference>
   </ItemGroup>
 
<Target Name="Build">
   <MakeDir Directories="$(OutputPath)"
            Condition="!Exists('$(OutputPath)')" />
   <Csc Sources="@(Compile)"
        References="@(Reference)"
        OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
   </Target>
</Project>

To make the process simpler, MSBuild reuses elements of the build process through the use of .targets files, which define a compile target which is the override for any defaults. For example, Microsoft.CSharp.targets defines how to build C# applications with MSBuild; a similar file exists for J# and VB in the .NET Framework 2.0 directory (as specified in the LIBPATH environment variable).