tbicr
tbicr

Reputation: 26090

How get exec task output with msbuild

I'm trying to get simple output by exec task with msbuild:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Test">
    <Exec Command="echo test output">
      <Output TaskParameter="Outputs" ItemName="Test1" />
    </Exec>
    <Exec Command="echo test output">
      <Output TaskParameter="Outputs" PropertyName="Test2" />
    </Exec>
    <Message Text="----------------------------------------"/>
    <Message Text="@(Test1)"/>
    <Message Text="----------------------------------------"/>
    <Message Text="$(Test2)"/>
    <Message Text="----------------------------------------"/>
  </Target>
</Project>

But get next output:

  echo test output
  test output
  echo test output
  test output
  ----------------------------------------
  ----------------------------------------
  ----------------------------------------

How can I get output by my script?

Upvotes: 90

Views: 47864

Answers (4)

Avi Cherry
Avi Cherry

Reputation: 4096

Good news everyone! You can now capture output from <Exec> as of .NET 4.5.

Like this:

<Exec ... ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" PropertyName="OutputOfExec" />
</Exec>

Simply:

  • Add ConsoleToMsBuild="true" to your <Exec> tag
  • Capture the output using the ConsoleOutput parameter in an <Output> tag

Finally!

Documentation here

Upvotes: 175

Frantisek Bachan
Frantisek Bachan

Reputation: 285

If you want to capture output to an array-like structure and not to a plain string where the output lines are separated by a semicolon, use ItemName instead of PropertyName:

<Exec ... ConsoleToMSBuild="true">
  <Output TaskParameter="ConsoleOutput" ItemName="OutputOfExec" />
</Exec>

Upvotes: 11

Syam
Syam

Reputation: 1621

You can pipe the output to a file so to speak, and read it back.

echo test output > somefile.txt

Upvotes: 0

Samer Adra
Samer Adra

Reputation: 693

I've gotten to the point where I'm so frustrated with the limitations of MSBuild, and the stuff that's supposed to work but doesn't (at least not in every context), that pretty much anytime I need to do anything with MSBuild, I create a custom build task in C#.

If none of the other suggestions are working, then you could certainly do it that way.

Upvotes: 7

Related Questions