Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget MsBuild Integration - Snapshot and Release versioning

Currently we are evaluating NuGet tool for our dot net project built on framework 4.6.2 using VS2017. Our goal is to refer our common libraries in our solution as reference (like Maven dependencies) pulling it from internal nexus repository.

We are familiar with maven concepts like snapshot and release. We would like to achieve the same using NuGet tool. Brief search on internet says this is not supported in Nuget. Any pointers to do it in elegant way for dotnet projects with NuGet?

Any assistance in this regards is appreciated. Thanks

like image 893
Abdul Jabbar Avatar asked Oct 18 '25 17:10

Abdul Jabbar


1 Answers

Using the new SDK-based project system, NuGet and MSBuild are integrated. There currently isn't a project template for this in VS2017 but you can create a .NET Standard library and change the <TargetFramework> value in the .csproj file to:

<TargetFramework>net462</TargetFramework>

Then you need to define a version number for your library. This can be done through setting a Versionproperty OR setting a combination of VersionPrefix and VersionSuffix property that are combined during build. For CI-szenarios, you usually want to set only the VersionPrefix value in your .csproj file, inside of a <PropertyGroup> element:

<VersionPrefix>1.2.3</VersionPrefix>

This can also be set inside a Directory.Build.props file in the solution directory to have a single place to set the property for all projects in one place:

<Project>
  <PropertyGroup>
    <VersionPrefix>1.2.3</VersionPrefix>
  </PropertyGroup>
</Project>

When building through CI or locally, you can set the VersionSuffix property through command line, the pack command of the dotnet cli offers a convenience option for this:

dotnet pack -c Release --version-suffix SNAPSHOT

Alternatively, the same result can be accomplished when using the VS version of MSBuild via the Developer Command Prompt:

msbuild /t:Pack /p:Configuration=Release /p:VersionSuffix=SNAPSHOT

Currently (VS 2017 15.2, .NET CLI 1.0.*), there is a bug when having multiple projects that reference each other - the dependency versions aren't generated with a the specified version suffix. There is a workaround: Perform an additional restore before packing using the same property:

dotnet msbuild "/t:Pack;Restore" /p:Configuration=Release /p:VersionSuffix=SNAPSHOT

On your CI system you would typically overwrite the Suffix with a build number to generate a version like 1.2.3-ci-20170102 or just generate SNAPSHOT / PREVIEW etc. packages.

Since msbuild allows for scripting, you can also extend the csproj file to set the version suffix automatically if some conditions are met - e.g. always generate a suffix for debug builds so you don't accidentally publish a debug build.

<VersionSuffix Condition=" '$(Configuration)' != 'Release' ">SNAPSHOT</VersionSuffix>
like image 136
Martin Ullrich Avatar answered Oct 21 '25 09:10

Martin Ullrich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!