MSBUILD: Targets, Tasks and Properties

MSBUILD – From the Start

MSBuild projects files are composed of XML. The simplest form of a project file contains only a Project element.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 </Project>

MSBUILD Properties

Properties of MSBUILD are key-value pairs – a term I’ve heard through working with PowerShell and JavaScript. Static properties that are declared must be done so within a PropertyGroup Element.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Key>value</Key>
        <Key>value</Key>
    </PropertyGroup>
</Project>

Another example with more meaningful key-value pairs and multiple PropertyGroups

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <AppServer>myApp.myServer.net</AppServer>
    </PropertyGroup>
    <PropertyGroup>
        <WebServer>myWeb.myServer.net</WebServer>
    </PropertyGroup>
</Project>

MSBUILD Tasks and Targets

MSBUILD utilizes two ‘execution elements’: Task and Target. A task is the smallest unit of work in MSBUILD and a Target is a collection of Tasks. MSBUILD comes with many tasks available such as Copy, Move, Exec and Message ( There’s a big list of them on the left-hand pane @ http://msdn.microsoft.com/en-us/library/7z253716.aspx ).

Here is a simple example of declaring a Target with a Task.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="HelloWorld">
        <Message Text="Hello world!" />
    </Target>
</Project>

If you save this into a .csproj file, it can be built by MSBUILD. I saved it as program.csproj and moved a copy of it into

C:\Program Files (x86)\Microsofot Visual Studio 12.0\

Which is why my Visual Studio installation lives – and so does MSBUILD. The reason for moving the csproj file into there was just because I didn’t want to enter in a full path name to the file (which was on another drive).

Now if you open a VS CMD or just a regular CMD and CD to the directory above, you can use MSBUILD on the file.

07292014_MSBUILD2

The syntax is like this: MSBULD <YourCSPROJ> /t:<TaskToRun>

When ran, the results will display as follows:

07292014_MSBUILD3

 

Using Properties, Tasks, and Targets together

So MSBUILD has the ability to declare Properties which can be encased in PropertyGroups and Targets that can implement a various number of Tasks. How are these used together? Like so~

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Target Name="HelloWorld">
        <Message Text="Hello world!" />
    </Target>

    <PropertyGroup>
        <HelloMessage>Hello World</HelloMessage>
    </PropertyGroup>
    <Target Name ="HelloFromPropertyGroup">
        <Message Text="$(HelloMessage)" />
    </Target>
</Project>

I left the original Target named ‘HelloWorld’ and added a new PropertyGroup which contains a property with the key ‘HelloMessage’ and value ‘Hello World’. A new Target named HelloFromPropertyGroup is then created with a Message Task. The Message Task proceeds to use the ‘HelloMessage’ property in the Text parameter.

In order to view the results of the new changes, save the file and then enter the new target in MSBuild’s /t parameter.

07292014_MSBUILD5

Once it builds the results display the text from the property.

07292014_MSBUILD6

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s