ripper234
ripper234

Reputation: 230376

Return output from an MsBuild task?

I'd like to calculate a path in a MsBuild task, to be used by another MsBuild task. What is the best way to accomplish this?

Setting a environment variable, printing to Console, ...?

Upvotes: 37

Views: 19000

Answers (1)

Julien Hoarau
Julien Hoarau

Reputation: 50000

Use a property or an item. Your MSBuild that calculates the path, return it as a property and you use this property as input for your other task.

public class CalculatePathTask : ITask
{
    [Output]
    public String Path { get; set; }

    public bool Execute()
    {                                   
        Path = CalculatePath();

        return true;
    }
}
<Target Name="CalculateAndUsePath">
  <CalculatePathTask>
    <Output TaskParameter="Path" PropertyName="CalculatePath"/>
  </CalculatePathTask>

  <Message Text="My path is $(CalculatePath)"/>
</Target>

If you need to pass a value between two MSBuild project, you should create a third one that will call the other using MSBuild Task and use the TargetOutputs element to get back the value that you want.

Upvotes: 54

Related Questions