Search code examples
c#.netversionnuget-package

.NET setting package version with version autoincrement


I am trying to create a NuGet package from my C# class library project. It uses automatic versioning set in AssemblyInfo.cs:

[assembly: AssemblyVersion("0.5.*")] 

But package information is taken from the Version property from the .csproj file.

Because of this I have to either update version twice in both files or remove version from AssemblyInfo.cs and stop using automatic versioning (looks like it is not supported in .csproj).

Is there any way to set package version (to 0.5 in my example) and keep automatic versioning of the file while updating version in only one file?


Solution

  • This answer shows that it is possible to have automatic AssemblyVersion increment in a .csproj only approach. Just including below one was sufficient for me.

    <Deterministic>false</Deterministic>
    

    In order to avoid having to adjust the fixed part of the version number -- here : 0.5 -- multiple times, you can set up a variable and reference it from within the other version tags.

    Below fragment of a .csproj file shows a variable _versionNumber, used in AssemblyVersion which adds the .* wildcard segment, and used as-is in Version and some more.

    <Project>
      <PropertyGroup>
        <Deterministic>false</Deterministic>
    
        <_versionNumber>0.5</_versionNumber>
        
        <AssemblyVersion>$(_versionNumber).*</AssemblyVersion>
        <Version>$(_versionNumber)</Version>
        
        <!-- Any other version attributes of interest -->
        <FileVersion>$(_versionNumber)</FileVersion>
        <InformationalVersion>$(_versionNumber)</InformationalVersion>
        
        <!-- More tags -->
      </PropertyGroup>  
    </Project>