DCSIMG
MSBuild Basics - Maor's Blog

MSBuild Basics

One of my favorite technologies 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:

  1. PropertyGroup
  2. ItemGroup
  3. Target
  4. Task

 

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.

<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>  

 

Technorati Tags:
Published 30 September 2007 03:10 AM by Maor David-Pur
תגים:

Comments

# Maor David's Blog said on 10 October, 2007 01:28 AM

This is the 2nd post in the msbuild posts series. The first post was an introduction to MSBuild and give

# Eugenez said on 10 October, 2007 06:24 PM

Not to discourage you - but here is pretty lenghty article that covers intro to MSBuild; and it is pretty good too.

en.csharp-online.net/MSBuild:_By_Example

# target » Blog Archive » MSBuild Basics said on 19 October, 2007 06:09 PM

Pingback from  target  &raquo; Blog Archive   &raquo; MSBuild Basics

# Maor David's Blog said on 24 October, 2007 10:53 AM

This is the 3rd post in the series of posts about MSBuild. You can read them at: Introduction to MSBuild

# Maor David said on 11 December, 2007 02:04 PM

Building non-MSBuild projects is possible. For example you have to build VS2003, Installer, C++, Delphi

# Maor David said on 17 May, 2008 04:55 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog&#39;s statistics, top posts and more.

# Maor David said on 17 May, 2008 04:59 PM

Last week it was exactly one year since I started blogging, so this is my blog first birthday!! Come and read about the blog&#39;s statistics, top posts and more.

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:

Search

Go

This Blog

News

    RSS

     

    Connect with Me

    Maor's Facebook profile  Follow Maor on Twitter  Maor's profile on Linkedin  Maor in FriendFeed 
           

Syndication