msbuildmsbuild-target

MSBuild Access Property Group declared in a Target outside


I'm having problem in accessing a PropertyGroup which is declared inside a target.

Message inside Target is publishing the version number correctly. When i try to publish the VersionNumber and VersionInfo ,I’m able to see only correct value for VersionInfo as true but VersionNumber is displayed as empty string.I want the VersionNumber value also published here

Please help !

Below is my code file :

<PropertyGroup >
  <FileLocation>C:\Dev\version.txt</FileLocation>
    <VersionInfo>false</VersionInfo>
    <VersionInfo Condition="Exists('C:\Dev\version.txt')">true</VersionInfo>
  </PropertyGroup>

<Target Name="ReadFromFile">
  <ReadLinesFromFile File="$(FileLocation)" >
    <Output PropertyName="VersionNumber"
        TaskParameter="Lines"/>
  </ReadLinesFromFile>
  <Message Text="Inside Target (Version Number) : $(VersionNumber)"/>
</Target>

<ItemDefinitionGroup>
  <PreBuildEvent>
    <Command>
      echo VersionNumber: $(VersionNumber)
      echo VersionInfo: $(VersionInfo)
    </Command>
  </PreBuildEvent>
</ItemDefinitionGroup>

Solution

  • I found a solution for my problem.Even i could remove the entire Target-ReadfromFile and could read the text file content in property group itself.I used property function - System.IO.File::ReadAllText to achieve my functionality. It turned out to be a simple solution

    More details about property functions can be found here

    My code looks like below now :

    <PropertyGroup >
      <FileLocation>C:\Dev\version.txt</FileLocation>
        <VersionInfo>false</VersionInfo>
        <VersionInfo Condition="Exists('C:\Dev\version.txt')">true</VersionInfo>
      <VersionDetails>$([System.IO.File]::ReadAllText($(FileLocation)))</VersionDetails>
      </PropertyGroup>
    

    Now i can access VersionDetails property anywhere in the project