36

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, ...?

ripper234
  • 222,824
  • 274
  • 634
  • 905

1 Answers1

54

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.

Yodan Tauber
  • 3,907
  • 2
  • 27
  • 48
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • 2
    just a short note: In my case `ItemName` was not working. As soon as I changed it to `PropertyName` it worked like a charm. – sebagomez Nov 26 '11 at 03:12
  • Tried this but got `The "TaskParameter" parameter is not supported by the "MyCustomTask" task. Verify the parameter exists on the task, and it is a settable public instance property.` – jpierson Feb 06 '17 at 20:38
  • @jpierson too late but for the future: maybe you forgot to mark the attribute as [Output] in the task. – JohnTortugo Apr 27 '18 at 18:51