Source

SetVersionTask /

Filename Size Date modified Message
SetVersionTask
SetVersionTaskTests
Tools
60 B
2.1 KB
1.4 KB
322 B

MSBuild SetVersion Task

This is a simple MSBuild task allowing you to set or update the assembly version of a C# file

Usage

<SetVersion FileName="AssemblyInfo.cs" AssemblyVersion="1.2.+.=" />

The rule "1.2.+.=" means: set the Major version to 1, set the Minor version to 2, increment the Build and leave the Revision at whatever it was before. The AssemblyVerision will be changed - all other attributes will be left unchanged.

Example

Here's a more complete example. We store the location of SetVersionTask.dll in a PropertyGroup. We've got an UpdateVersionNumber target that only runs if it is a release build and if SetVersionTask.dll can be found. Then we increment the build number in for both AssemblyVersion and AssemblyFileVersion. Since this example is in a csproj file, for our target to be automatically be called before a Release build from Visual Studio, we make the built-in "BeforeBuild" Target depend on our UpdateVersionNumber target.

<PropertyGroup>
  <SetVersionPath>..\Tools\SetVersionTask.dll</SetVersionPath>
</PropertyGroup>
<UsingTask TaskName="SetVersion" AssemblyFile="$(SetVersionPath)" />
<Target Name="UpdateVersionNumber" Condition="Exists('$(SetVersionPath)') AND '$(Configuration)' == 'Release'">
  <Message Text="Updating Version..." />
  <SetVersion FileName="Properties\AssemblyInfo.cs" AssemblyVersion="=.=.+.=" AssemblyFileVersion="=.=.+.=" />
</Target>
<Target Name="BeforeBuild" DependsOnTargets="UpdateVersionNumber">  
</Target>

Note: If you are compiling from within Visual Studio and have the AssemblyInfo.cs file open, then although AssemblyInfo.cs will get updated, the build will use the version of AssemblyInfo.cs as it was when you launched compilation. The workaround is to leave AssemblyInfo.cs closed when building, or build from the command line using msbuild. If I can find a fix to this I will include it in the next version. Another workaround is described here