Search code examples
msbuildmsbuild-task

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


Solution

  • 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.