Creating Your First Task

As many other texts do, we will create a Hello World example. This simple example, which follows, was taken from the HelloWorld.cs file.

public class HelloWorld : ITask
{
    public IBuildEngine BuildEngine
    { get; set; }
    public ITaskHost HostObject
    { get; set; }

    public bool Execute()
    {
        // set up support for logging
        TaskLoggingHelper loggingHelper = new TaskLoggingHelper(this);
        loggingHelper.LogMessageFromText(
           "Hello MSBuild", MessageImportance.High);

        return true;
     }
}

In this first example, we have created a HelloWorld task that outputs the message Hello MSBuild to the loggers attached to MSBuild. This is achieved by using an instance of the TaskLoggingHelper class. In this case, we have directly implemented the ITask interface. ...

Get Inside the Microsoft® Build Engine: Using MSBuild and Team Foundation Build now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.