mohamedradwan.com - Nothing can beat experience
Post
Cancel

Create MSBuild Custom Task

Hello, I Will show you how to create MSBuild custom task I know you will find many examples on the internet but if you just try most of them most properly it will not work, so I decide to post article that describe it in very simple way with source code of the success project. If you want to see a tutorial on MSBuild you can visit the following link click-here

First part

  • Create a class library project that will contain all your custom task classes
  • Reference both Microsoft.Build.Framework and Microsoft.Build.Utilities
  • Create a new class and name it MyTask inherit from Class Task, and override the Execute method
  • Call Log.LogMessage to output a message during the build and give it the property of your class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace CustomTask 
{
    public class MyTask : Task 
    {
        public string MyProperty { get; set; }

        public override bool Execute() 
        {
            // Log a high-importance comment
            Log.LogMessage(MessageImportance.High, "The task was passed \"" + MyProperty + "\".");
            return true;
        }
    }
}

  • Create any project,  it prefers to be class library also and remove all the code between the project element
  • Edit the target to be MyTarget
  • Add a reference to the assembly that contain the custom task and here what cause the problem to happen, the wrong path always make problem so my advice to you that you have to know exactly how to point to specific file and what is the different between absolute and relative path, how to out from the current folder or 2 folders and so on so forth, any way you just add the reference
  • Add new Target called MyTarget and start using the task

Use your custom task

  • Open Visual studio CMD and go the project file path and run MSBuild and that's it

Run MSBuild for the project

  • Run MSBuild for the project[/caption] To download the project just click here

  • You can find also the link on the MSDN that describe the custom task here

Most of problem happen because the path of the dll of the custom task so if you have a problem creating your own you just use absolute path to make sure the example work and then fix the absolute path with relative path I hope you like it.

Trending Tags