11

Can someone write (or link to) a walkthrough that explains exactly how to create a custom MSBuild task and run it during a build? I'm looking for a custom task that inherits from Microsoft.Build.Utilities.Task and does only this:

public override bool Execute()
{
    Log.LogMessage("Hello world!");
    return true;
}

(I've been working on this for hours and keep getting the "The [whatever] task was not found. Check the following" message. I think I must be missing an essential step somewhere. If there's a clear tutorial I can follow, perhaps I'll figure out where I'm falling short.)

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • 1
    Are you including in a ? Can you post the MSBUILD where you try to invoke along with the full class for the task? – Taylor Bird Mar 02 '11 at 16:55
  • 1
    Well, heck. It turns out my stupid build task *was* working. But Log.LogMessage doesn't display anything in the build window if the build verbosity is set to the default setting Minimal. http://social.msdn.microsoft.com/Forums/en/msbuild/thread/9f60e577-a420-4302-ba5f-703a6cae7992 – Ryan Lundy Mar 02 '11 at 16:57
  • 1
    ...Unless I use the overload that lets me specify `MessageImportance.High`, and then it displays even with Minimal. – Ryan Lundy Mar 02 '11 at 17:00
  • Although the OP's original need has been solved, nothing really answered the question of providing a "hello world" example. That would be really useful still. – StayOnTarget Apr 12 '18 at 16:07

2 Answers2

12

See

  1. Best Practices For Creating Reliable Builds section Creating Custom Tasks.
  2. THE CUSTOM MSBUILD TASK COOKBOOK from Bart De Smet
Stephen Klancher
  • 1,374
  • 15
  • 24
Sergio Rykov
  • 4,176
  • 25
  • 23
  • 1
    Thanks for this. Too many resources such as https://learn.microsoft.com/en-us/visualstudio/msbuild/task-writing?view=vs-2017 provide C# and MSBuild code but provide no information on what to do with the C# code so you can actually use it. They do not give the critical piece of information that you need to build a DLL. – Benilda Key Jan 27 '19 at 23:08
10

Are you declaring your custom task in your MSBuild project file? You need a line like this:

    <UsingTask AssemblyFile="C:\PathTo\MyTasks.dll" TaskName="MyTasks.HelloWord" />

Then MSBuild can execute your task.

Carl Raymond
  • 4,429
  • 2
  • 25
  • 39
  • 1
    I was doing that, but I didn't realize that TaskName had to be the actual class name including namespace. That seems to be the thing that was my roadblock. – Ryan Lundy Mar 02 '11 at 18:38