Building NuGet packages from Visual Studio

Am I going about this wrong?

This GeneratePackageOnBuild property is not something for .net framework projects. Create a new .net Standard or .net Core class library project, right-click the project you'll see a Pack command(not available for .net framework).

enter image description here

If we click the button, VS will pack that assembly into a nuget package. And if someone doesn't want to click that button every time manually, go Project=>Properties=>Package we can see the Generate Nuget Packages on build checkbox. Enable it, and then the nuget package will be created after every build.

Actually, enabling that checkbox in VS will add statement <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to xx.csproj, but the Pack button or Package tab in Project=>Properties are not available for .net framework projects.

So I'm afraid the answer is negative, you should't use that property for .net framework projects.(I tested the property in VS2017 and VS2019, it all just did nothing, can't reproduce the partial work mentioned in your question)

Is there an easy way to do this from within Visual Studio?

You need to use nuget pack command to do that as Lex Li says. And to do this in VS automatically, you can consider configuring that command in Post-Build-Event or using custom after-build target to run that command.

Since you want these NuGet packages to pack automatically from Visual Studio during the release build. You can try adding this script into your xx.csproj file:

  <Target Name="CustomPack" AfterTargets="build" Condition="'$(Configuration)'=='Release'">
    <Message Text="Custom Pack command starts ..." Importance="high"/>
    <Exec Command="nuget pack $(MSBuildProjectFile) -Properties Configuration=Release"/>
  </Target>

To run this script successfully, you need to download the nuget.exe and add the path of it to Environment Variables. Or use the full path like "C:\SomePath\Nuget.exe" pack ...

If the build in release mode succeeds, you'll see a xx.nupkg file in project folder.

In addition:

1.For more details about nuget pack command please see this document.

2.And don't forget to create a xx.nuspec file in project folder to avoid encountering warnings like NU5115(xxx was not specified). Similar issue see here.


To enable on-build packing in a "non SDK" project, using old .NET framework (eg:. 4.5.1) and visual studio 2019 without using custom Target. you need to do the following step:

  1. add a first PropertyGroup tag on the csproj
  2. add minimal tags Authors and PackageOutputPath
  3. check that the same PropertyGroup has GeneratePackageOnBuild

Now the variables will be passed to the internally triggered msbuild -t:Pack command.

Here an example of working configuration, please make sure this will be the first <PropertyGroup> of the .csproj:

  <PropertyGroup>
    <Title>packageid</Title>
    <Description>your description</Description>
    <Version>1.1.1</Version>
    <ReleaseNotes>New package system</ReleaseNotes>
    <Authors>authors</Authors>
    <Owners>owners</Owners>
    <Copyright>your copyrights</Copyright>
    <PackageOutputPath>bin\Package</PackageOutputPath>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

If you are looking for the references, here:

  • how to create a nuget package with msbuild
  • all possible tags to set on the csproj on the first PropertyGroup