How get exec task output with msbuild

无人久伴 提交于 2019-11-26 10:27:23

问题


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?


回答1:


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




回答2:


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.




回答3:


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>



回答4:


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

echo test output > somefile.txt


来源:https://stackoverflow.com/questions/8938679/how-get-exec-task-output-with-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!