First posted at: http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/09/30/msbuild-basics.aspx
One of the most technologies I like is MSBuild. I found that I didn't dedicate too much writing time about it in this blog. I think and feel that I really neglect MSBuild at my blog...:(
This post will be the first of series of posts about this technology.
So lets start with the basics of MSBuild.
The Microsoft Build Engine (MSBuild) is the new build platform for Microsoft and Visual Studio. MSBuild is completely transparent with regards to how it processes and builds software, enabling developers to orchestrate and build products in build lab environments where Visual Studio is not installed.
Quote from Microsoft site.
In principle, this definition is really injustice for MSBuild. When I read it I insulted for MSBuild. For me, MSBuild is a high level scripting technology. I can use MSBuild almost for everything. NOT only build!
The introduction of MSBuild as an official utility was very welcome among the development community as it provides close integration with the existing project and solution files created by Visual Studio. This close integration cuts down on the amount of detail necessary for the build scripts.T he MSBuild utility comes with .NET 2.0 and is available with the runtime even if Visual Studio is not installed.
MSBuild based on XML-based project. (like csproj,vbproj...)
The basic elements of the MSBuild project file format are:
- PropertyGroup
- ItemGroup
- Target
- Tasks
PropertyGroup
Properties are defined in the PropertyGroup
element. You can simply add an arbitrary element and enclose the assigned value within that element. Properties represent key/value pairs that can be used to configure builds. Example of PropertyGroup:
<PropertyGroup>
<AppName>My application name</AppName>
<AppDir>C:\Apps</AppDir>
<ThirdPartyDir>C:\3rdParty</ThirdPartyDir>
</PropertyGroup>
ItemGroup
Defining lists is done within an ItemGroup
element. You will want to use a list with a Task occasionally, such as copying a group of files to another folder. Below is an example of an ItemGroup
.Items are declared in the project file by creating an element with the name of the item collection as a child of an ItemGroup element. Example of ItemGroup:
<ItemGroup>
<FilesToCopy Include = "file1.cs"/>
<FilesToCopy Include = "file2.cs"/>
</ItemGroup>
You reference item collections throughout the project file with the syntax @(ItemGroupName). In thus case, you reference the item group with @(FilesToCopy).
Targets
Targets group tasks together in a particular order and expose sections of the project file as entry points into the build process. Targets are often grouped into logical sections to allow for expansion and increase readability.
Targets are declared in the project file with the Target element. For example, the target bellow occurs after build finished.
<Target Name="AfterBuild">
Do something....
</Target>
Tasks
Tasks are reusable units of executable code used by MSBuild projects to perform build operations. For example, a task might create directories and copy files. Once created, tasks can be shared and reused.
You execute a task in an MSBuild project file by creating an element with the name of the task as a child of a Target element.
MSBuild shipped with a alot of built in tasks. For example: MakeDir,Copy and more.
See below an example for using tasks:
<Target Name="AfterBuild">
<MakeDir Directories="$(AppDir)" />
<Copy SourceFiles="$(FilesToCopy)" DestinationFolder="$(AppDir)" />
</Target>
Next in the series I'll dive deeper with the msbuild script.
The msbuild script below is a demonstration about my earlier sentence: "I can use MSBuild almost for everything". Save this as .proj file, and execute it from VS2005 command prompt as: msbuild YourSavedFileName
This script only opens windows calculator...
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Build" >
<Message Text="Executing calculator" />
<Exec Command="calc" />
</Target>
</Project>