5

Is there a way to auto-increment the NuGet package version when using "Generate NuGetPackage on Build" in C#/.NET Standard 2.1?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
  • Build in VS or something like Azure Devops? If you use git to manage the version, you can consider using tool like [GitVersion](https://gitversion.readthedocs.io/en/stable/usage/msbuild-task/). – LoLance Oct 04 '19 at 06:00
  • Hi Jonathan, any update for this issue? – LoLance Oct 10 '19 at 03:46

2 Answers2

4

If you build and publish the project by a build server, and manage the version by Git, you can try the MSBuild GitVersion task. For a similar issue, see here.

But if you just want a clean build and publish in local Visual Studio without using Git, then I'm afraid for now there isn't any Visual Studio option or MSBuild task to support this behavior. You can go to the Developer Community to suggest the feature because it's a meaningful idea, but currently it's not available.

Also, as from hints from Martin and Alexandre, you can add this script into xx.csproj:

  <PropertyGroup>
    <GenerateNuspecDependsOn>$(GenerateNuspecDependsOn);SetPackageVersion</GenerateNuspecDependsOn>
  </PropertyGroup>

  <Target Name="SetPackageVersion" DependsOnTargets="Build">
    <PropertyGroup>
      <!-- <PackageVersion>$([System.DateTime]::Now.ToString(&quot;yyyy.MM.dd.HHmmss&quot;))</PackageVersion> -->
      <!-- You can customize the format and the rule about how version increases here. -->
      <PackageVersion>$([System.DateTime]::Now.ToString(&quot;1.MM.dd&quot;))</PackageVersion>
    </PropertyGroup>
  </Target>

Then when you publish the package today, its version is 1.10.4. Tomorrow for 1.10.5, it works if you don't really need to publish different versions of one package in a day. Also you can customize the script to define your version-increasing rule.


If targeting multiple frameworks, see Build project with multiple targetframeworks in TFS as a NuGet package

This works when setting the assembly version to wildcard.

<Deterministic>false</Deterministic>
<AssemblyVersion>3.0.*</AssemblyVersion>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LoLance
  • 25,666
  • 1
  • 39
  • 73
1

As of toDate with Visual Studio 2019 and .NET 5.0, what LoLance suggested about scripting the increment inside the .csproj file still works, and we now can use shell commands directly inside the package definition's PropertyGroup for multiple fields, as shown in the picture below:

Proof of concept

Andreas M.
  • 182
  • 1
  • 10