1

How should I automatically increment the version number because the wildcard '*' is no longer allowed in the. NET 7 project file (. csproj)? For example,1.0.0.0, the version number automatically increases by 1 every time I debug, generate, or publish. When 1.0.0.9999, it automatically advances by 1, 1.0.1.0;

I have used the following methods in the. NET 7 project file (. csproj), but they have not been effective

<PropertyGroup>
<MajorVersion>1</MajorVersion>
        <MinorVersion>0</MinorVersion>
        <PatchVersion>0</PatchVersion>
        <BuildVersion>0</BuildVersion>
        <Version>$(MajorVersion).$(MinorVersion).$(PatchVersion).$(BuildVersion)</Version>
</PropertyGroup>

<Target Name="IncrementVersion" BeforeTargets="Build">
        <PropertyGroup>
            <BuildVersion>$([MSBuild]::Add($(BuildVersion), 1))</BuildVersion>
        </PropertyGroup>
    </Target>
  • I personally like [Unclassified.NetRevisionTask](https://www.nuget.org/packages/Unclassified.NetRevisionTask) as it reflects the version tag from git – Sir Rufo May 30 '23 at 10:51
  • We use [GitVersion](https://gitversion.net/docs/) in our team. It can do [assembly patching](https://gitversion.net/docs/usage/cli/assembly-patch) but we usually just set the MSBuild `Version` property to the value it returned in our CI pipeline when building/publishing. Assembly patching will change `AssemblyInfo.cs` files which causes working tree changes and we don't want that during the CI. – Good Night Nerd Pride May 30 '23 at 13:12

1 Answers1

1

I used once custom .tt script file, and run i it on build. you can pesonalise what happen, in my case i put the current date in it.

note that if you skip the <Import> in .csproj you can do it manually by execute the .tt script file.

can't find initial post where i find this solution sorry

in .csproj file :

<PropertyGroup>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  <TransformOnBuild>true</TransformOnBuild>
  <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles><!-- not shure if this line is necessary-->
  <TransformOutOfDateOnly>false</TransformOutOfDateOnly><!-- not shure if this line is necessary-->
</PropertyGroup>

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v17.0\TextTemplating\Microsoft.TextTemplating.targets" />

in a file "Assembly.tt" :

<#@ template language="C#" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>

using System.Reflection;

[assembly: AssemblyVersion("<#= this.lMajor #>.<#= this.lMinor #>.<#= this.lBuild #>.<#= this.lRevision #>")]
[assembly: AssemblyFileVersion("<#= this.lMajor #>.<#= this.lMinor #>.<#= this.lBuild #>.<#= this.lRevision #>")]
<#+
    string lMajor =  DateTime.Now.ToString("yyyy");    
    string lMinor =  DateTime.Now.ToString("MM");
    string lBuild = DateTime.Now.ToString("dd");
    int lRevision = (int)(DateTime.UtcNow - new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, DateTime.UtcNow.Day)).TotalMinutes;
#>
Arnaud
  • 64
  • 5