nrofis
nrofis

Reputation: 9766

Read build parameters in MSBuild task

I'm trying to create some tool that need to run script after the target project build complete. In order to do that, in my tool I've created also MSBuild task.

My problem is that I need to know what is the ConfigurationName and TargetPath variables in my tasks.

public class MyTask : Task
{
    public override bool Execute()
    {
        var output = // TargetPath variable
        var configuration = // ConfigurationName variable
        RunScript(output, configuration);
        return true;
    }
}

How can I read build variables in MSBuild task?

Upvotes: 2

Views: 836

Answers (1)

Christian.K
Christian.K

Reputation: 49230

The most robust way to do this, is to simply pass such properties into your task as its own properties:

public class MyTask : Task
{
    [Required]
    public string ConfigurationName { get; set; }
    [Required]
    public string TargetPath { get; set; }

    public override bool Execute()
    {
        var output = this.TargetPath; // TargetPath variable
        var configuration = this.ConfigurationName; // ConfigurationName variable
        RunScript(output, configuration);
        return true;
    }
}

You can declare them as "required" (see [Required] attributes above), or not. Depending on your needs.

Then set them accordingly from the .targets or .*proj file:

<MyTask
    Configuration="$(Configuration)"
    TargetPath="$(... whatever ...)"
/>

Upvotes: 1

Related Questions