如何使用 msbuild 获得 exec 任务输出

我试图用 msbuild通过 exec 任务获得简单的输出:

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

但下一个输出结果是:

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

如何通过脚本获得输出?

40997 次浏览

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.

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

echo test output > somefile.txt

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

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>