How to Write a Custom MSBuild Task

When working on a chapter of my upcoming Professional Visual Studio Add-ins and Extensions book, I wrote about MSBuild tasks and writing a custom task for custom scenarios.  Let me write a short description about it here as well.

Writing a custom MSBuild task consists of creating a class in an assembly which implements ITask interface located in Microsoft.Build.Framework assembly.  This ITask interface class has two properties and a method to implement.  BuildEngine and HostObject are two properties to implement and Execute is a Boolean function to implement.

Not only you can implement this interface but also alternatively Microsoft has done a default implementation for BuildEngine and HostObject properties in Task abstract base class located in Microsoft.Build.Utilities assembly which you can use to override its Execute function and do same job.  Obviously this is easier and I'm going to use this approach here.

First I create a class library project with a class and add reference to Microsof.Build.Utilities assembly.  Now I derive my class from Task abstract base class and implement this base class.  This implementation shouldn't have anything except an overridden Execute method.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Build.Utilities;

 

namespace MyTask

{

    public class MyTask : Task

    {

        public override bool Execute()

        {

        }

    }

}

Execute function must return true if task has executed successfully otherwise it must return false.  Here I override this function with a very simple logic to just log a string value as you see below.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Build.Utilities;

 

namespace MyTask

{

    public class MyTask : Task

    {

        public override bool Execute()

        {

            Log.LogMessage("Keyvan Nayyeri");

 

            return true;

        }

    }

}

Now I compile this code into the assembly to get a DLL file.  After this I can write an MSBuild file to test my task.

To import this custom task to my MSBuild file I need to use a <UsingTask> element which is a direct child of root <Project> element.  This element has a TaskName attribute to get the name of the task class and an AssemblyFile attribute to get the name or address of the assembly where this class is implemented in.

So I create a very simple MSBuild file which uses a <UsingTask> element to import my custom task and has a single target to use this task.

<?xml version="1.0" encoding="utf-8"?>

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

  <UsingTask TaskName="MyTask"

            AssemblyFile="C:\Users\Keyvan Nayyeri\Documents\Visual Studio 2008\Projects\MyTask\MyTask\bin\Debug\MyTask.dll" />

  <Target Name="MyTaskTarget">

    <MyTask />

  </Target>

</Project>

Running this MSBuild file, I get an output like what you see below.  Notice the "Keyvan Nayyeri" text in the output.

MSBuild Output

That's it!  I've covered this in the book in more details.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

No Comments : 09.07.07

Feedbacks

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment