How to Update Files With Version in MSBuild

This article shows how to read the version from a version file and update version information of Files in MSBuild. MSBuild Community Tasks offer useful classes that can retrieve version information to 4 version fields, and update target files with them.

This can be used in updating Assembly Info files or XML format files during the build.

Import MSBuild Community Tasks

Before importing, make sure that you have installed MSBuild.Community.Tasks that can be downloaded from github or from Nuget.

MSBuild Community Task describes the useful open-source tasks for MSBuild. Import it first so as to use functional tasks from it.

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

Read Version with Version Class

Version is a class task from the Community Task. Read version information from a file and output 4 properties for Version information.

If you want to input Version information as a parameter in the MSBuild command line, you can also do. In this case, instead of separating Major, minor, build, and revision, you can use a single property such as $Build_Version=”1.2.3.4″

<Version VersionFile="Version.txt">
	<Output TaskParameter="Major" PropertyName="Major" />
	<Output TaskParameter="Minor" PropertyName="Minor" />
	<Output TaskParameter="Build" PropertyName="Build" />
	<Output TaskParameter="Revision" PropertyName="Revision" />
</Version>
<Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/>

Update a file with FileUpdate Class

FileUpdate is a Class from the Community Task, replacing text using Regular Expression.

Updating AssemblyInfo
<FileUpdate Files="Properties\AssemblyInfo.cs"
            Regex="(\d+)\.(\d+)\.(\d+).(\d+)"
	    ReplacementText="$(Major).$(Minor).$(Build).$(Revision)"
/>

Set Value in XML Query

XmlPoke is an MSBuild task that sets values specified in XPath Query. If version information is to be replaced in XML, you can use this.

<XmlPoke XmlInputPath="CustomAction.config"
	 Query="//add[@key='AppVersion']/@value"
	 Value="$(Major).$(Minor).$(Build).$(Revision)"/>

Run MSBUILD Target

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

  <Target Name="UpdateVersion">
    <Version VersionFile="Version.txt" >
      <Output TaskParameter="Major" PropertyName="Major" />
      <Output TaskParameter="Minor" PropertyName="Minor" />
      <Output TaskParameter="Build" PropertyName="Build" />
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </Version>
    <Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)"/>

    <FileUpdate Files="Properties\AssemblyInfo.cs"
            Regex="(\d+)\.(\d+)\.(\d+).(\d+)"
	    ReplacementText="$(Major).$(Minor).$(Build).$(Revision)"
				   />
    <XmlPoke XmlInputPath="CustomAction.config"
	     Query="//add[@key='AppVersion']/@value"
	     Value="$(Major).$(Minor).$(Build).$(Revision)"/>
  </Target>

</Project>

Run:

You may also be interested in the article: How to Create C# Inline Task in MSBuild

Leave a Comment