Johan's Blog

This and that in a developer's life in general
Welcome to Blogs @ IRM Sign in | Join | Help
 Search

Disclaimer

The content of this site is my own personal opinion and does not in any way represent my employer, it's subsideries or affiliates. These postings are provided "AS IS" with no warranties, and confer no rights.

This Blog

[MsBuild] Writing and Debugging Custom Tasks

Writing a custom task for MSBuild is very simple, here's the bare minimum code you would need to get you going:

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Build.Framework;

using Microsoft.Build.Utilities;

namespace IRM.Tasks

{

    public class HelloTask : Task

    {

        private string name;

        [Required]

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        public override bool Execute()

        {

            Log.LogMessage("Hello " + name);

            return true;

        }

    }

}

The task will obviously print out the value of the "Name" property to the log. 

To test it, create a test-project like a dummy console app and add this to the end of the .proj file:

<UsingTask TaskName="IRM.Tasks.HelloTask"    AssemblyFile="C:\code\HelloTask\bin\Debug\IRM.Tasks.dll" />

<Target Name="AfterBuild">

      <HelloTask Name="Johan" />

</Target>

If you unload the project from the Solution Explorer window you can Edit it directly in VS. 

But if you want to debug the task and set breakpoints in it? It was described by Neil Enns of the MSBuild team quite some time ago. After you have created a test-project and added the custom task to the .proj file as described above, do this:

  1. Open your custom task project
  2. Go to Project > projectname Properties…
  3. Go to the Debug tab
  4. Change the start action to “Start external program” and put in the full path to MSBuild as the program. In my case the path is “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe”
  5. Change the “Command line arguments” to the full path to your test project
  6. Close the property window

Now just start debugging your custom task, MSBuild should start up and start to build your test-project and will stop at any breakpoints you set in your custom task code. Neat!

Published den 23 februari 2007 09:27 by johan
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit
Powered by Community Server, by Telligent Systems