views:

307

answers:

1

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

+2  A: 

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" ItemName="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.

madgnome