tags:

views:

86

answers:

3

I've got a program that outputs to a file. I'm running it from an MSBuild project. I'd like this output to be written to StdOut, so that it can be picked up by our build agent (TeamCity).

How do I get MSBuild to dump the content of a file to the output?

A: 

You should be able to do something like this in your build script (note that I'm using the cat command that comes with cygwin here to output the file contents). You can change the target to whatever is appropriate depending on when you want the item to run in your build process:

<Target Name="AfterGet">
    <Exec Command="cat your_file" />
</Target>

If you need to install cygwin on your server, you can get it here. I do not know of a native dos command to echo file contents, but there may be one.

dcp
+1  A: 

The dos command type could to that.

<Target Name="ExecProgramAndOutputToStdOut">
  <Exec Command="YourProgram.exe"/>

  <Exec Command="type output_file"/>
</Target>
madgnome
The important thing here (which I didn't know) is that Exec always (?) uses CMD.EXE, which means that built-in commands (such as `type`) and redirection work inside `<Exec>`.
Roger Lipscombe
A: 

If you know where the file is that was written to you can use the ReadLinesFromFile task and then log all the messages. For example take a look at the project file below.

<Project DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    <PropertyGroup>
        <_File>$(MSBuildProjectFullPath)</_File>
    </PropertyGroup>
    <Target Name="Demo">
        <ReadLinesFromFile File="$(_File)">
            <Output ItemName="_FileContents" TaskParameter="Lines"/>
        </ReadLinesFromFile>

        <Message Text="File contents: '$(MSBuildProjectFullPath)'"/>
        <!-- Prints one after another with a ';' between each line -->
        <Message Text="@(_FileContents)"/>

        <Message Text="-------------"/>
        <!-- Prints one after another with each on its own line -->
        <Message Text="%(_FileContents.Identity)"/>
    </Target>
</Project>

This file reads the current file (via $(MSBuildProjectFullPath)) and prints the results to the console. I've showed how to print it out in 2 ways, one shows the ; separated values and the other shows one on a line of its own. Note that the ReadLinesFromFile task doesn't preserve leading (and maybe even trailing) white space.

Here is the result when I execute the Demo target.

C:\Data\Development\My Code\Community\MSBuild>msbuild ReadLines.proj /nologo
Build started 5/6/2010 6:29:43 PM.
Project "C:\Data\Development\My Code\Community\MSBuild\ReadLines.proj" on node 1 (default targets).
Demo:
  File contents: 'C:\Data\Development\My Code\Community\MSBuild\ReadLines.proj'
  <Project DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;;&lt;PropertyGroup&gt;;&lt;_Fi
  le>$(MSBuildProjectFullPath)</_File>;</PropertyGroup>;<Target Name="Demo">;<ReadLinesFromFile File="$(_File)">;<
  Output ItemName="_FileContents" TaskParameter="Lines"/>;</ReadLinesFromFile>;<Message Text="File contents: '$(MS
  BuildProjectFullPath)'"/>;<!-- Prints one after another with a ';' between each line -->;<Message Text="@(_FileC
  ontents)"/>;<Message Text="-------------"/>;<!-- Prints one after another with each on its own line -->;<Message
   Text="%(_FileContents.Identity)"/>;</Target>;</Project>
  -------------
  <Project DefaultTargets="Demo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
  <PropertyGroup>
  <_File>$(MSBuildProjectFullPath)</_File>
  </PropertyGroup>
  <Target Name="Demo">
  <ReadLinesFromFile File="$(_File)">
  <Output ItemName="_FileContents" TaskParameter="Lines"/>
  </ReadLinesFromFile>
  <Message Text="File contents: '$(MSBuildProjectFullPath)'"/>
  <!-- Prints one after another with a ';' between each line -->
  <Message Text="@(_FileContents)"/>
  <Message Text="-------------"/>
  <!-- Prints one after another with each on its own line -->
  <Message Text="%(_FileContents.Identity)"/>
  </Target>
  </Project>
Done Building Project "C:\Data\Development\My Code\Community\MSBuild\ReadLines.proj" (default targets).
Sayed Ibrahim Hashimi