MSBuild custom task "Hello World" walkthrough
Asked Answered
M

2

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.)

Melloney answered 2/3, 2011 at 16:32 Comment(4)
Are you including in a <UsingTask>? Can you post the MSBUILD where you try to invoke along with the full class for the task?Shewchuk
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. social.msdn.microsoft.com/Forums/en/msbuild/thread/…Melloney
...Unless I use the overload that lets me specify MessageImportance.High, and then it displays even with Minimal.Melloney
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.Determination
B
11

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.

Belief answered 2/3, 2011 at 16:54 Comment(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.Melloney
H
12

See

  1. Best Practices For Creating Reliable Builds section Creating Custom Tasks.
  2. THE CUSTOM MSBUILD TASK COOKBOOK from Bart De Smet
Haemocyte answered 2/3, 2011 at 16:55 Comment(1)
Thanks for this. Too many resources such as learn.microsoft.com/en-us/visualstudio/msbuild/… 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.Malchy
B
11

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.

Belief answered 2/3, 2011 at 16:54 Comment(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.Melloney

© 2022 - 2024 — McMap. All rights reserved.