MSBuild - build automation : part 2
Continuing on with MSBuild as a build automation system, there’s a few fundamentals to be aware of.
- PropertyGroup : containing Properties. These are referenced using $(PropertyName) : from Microsoft: Properties are key/value pairs that can be used to configure builds. Properties are useful for passing values to tasks, evaluating in conditions, and storing values that will be referenced thoughout the project file.
- ItemGroup: containing Items. These are referenced using @(ItemName) : from Microsoft: Items represent inputs into the build system that are grouped into item collections based on their user-defined collection names. These item collections can be used as parameters for tasks, which use the individual items contained in the collection to perform the steps of the build process.
- Targets: These conain the Tasks to execute. : from Microsoft: Targets group tasks together in a particular order and allow sections of the build process to be called from the command line. Targets are often grouped into logical sections to allow for project file expansion and to increase readability. For example, one target may delete all files in the output directory to prepare for the build, while another compiles the inputs for the project and places them in the empty directory.
- Task: The step to be executed/actioned. : from Microsoft: build platform needs the ability to execute any number of actions during the build process. MSBuild uses tasks to perform these actions. A task is a unit of executable code used by MSBuild to perform atomic build operations.
So lets put together what we have so far (from part 1), and put a basic build script together. Nothing too fancy just the standard hello world. A basic script that will output some text to the console window. First off, I usually create a batch file with the following in it :
%comspec% /k “”c:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat”" x86. This would allow us to run the msbuild file from our current location. Next off, Visual studio is about the best editor for the build file. It provides much needed intellisense support. You can use any text editor, Notepad, Notepad ++ etc.
So fire up the editor and churn out the bits below. First off, put in the xml declaration and Project node. Notice the DefaultTargets. This points to the Target that must be called to start off with. Next off, we add the Property group with a single property. This will contain the text we want to output. Create the Target node and name it go. MsBuild offers a host of prebuild Tasks and one of them we will use is Message task. Save this file in the same folder as the startup batch script. Call it helloworld.buildFile.
<?xml version=”1.0″?>
<Project xmlns=”http://schemas.microsoft.com/developer/msbuild/2003″ DefaultTargets=”go”>
<PropertyGroup>
<text_to_print>Hello msbuild World .. </text_to_print>
</PropertyGroup>
<Target Name=”go” >
<Message Text =”$(text_to_print)” />
</Target>
</Project>
To run this, fire up the batch file (the one from above), then at the command prompt type in msbuild helloworld.buildFile.

