Kirk Liemohn
Kirk Liemohn

Reputation: 7923

Using MSBuild, how do I set a property to the contents of a file?

I have a file that I set using PowerShell that contains the version number of my build. I need to get this within MSBuild so I can act on it within my build script. It seems simple enough; I just want to take the contents of the file and set a property to that value.

I thought maybe doing an Exec task, doing a "more" on my file, and capturing standard out would do the trick, but I can't seem to get this to work. It appears that others have had problems with stdout and MSBuild as well. Here is what I have tried:

<Exec Command="more $(BuildDirectory)\version.txt" Outputs="stdout">
    <Output TaskParameter="Outputs" ItemName="BuildNumber" />
</Exec>

Upvotes: 8

Views: 5198

Answers (1)

Scott Weinstein
Scott Weinstein

Reputation: 19117

The ReadLinesFromFile task is what you want

<ReadLinesFromFile File="Version.Txt">
    <Output TaskParameter="Lines" ItemName="BuildNumber"/>
</ReadLinesFromFile>

that said, another way to do what your question implies is to store you build num info in an xml file, with a MSBuild schema

something like

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
 <PropertyGroup>
   <BuildNumber>10</BuildNumber>
   <RevNumber>5</RevNumber>
 </PropertyGroup>
</Project>

and then import the version.properties file into your main msbuild file

Upvotes: 9

Related Questions