Problem
How to create and publish NuGet package in .NET Core
Solution
First setup is to have a registry setup to host the packages, I’ll use nuget.org.
Register a free account at www.nuget.org
Go to “API Keys” section of your account:
Create a new key by giving it a name, scopes (permissions) and selecting packages (* means all):
You’ll see a new key added, copy the key by clicking on the “Copy” link and paste it somewhere safe:
Now that you have a registry and key to upload your packages, let’s create and push a package. I am using here a simple .NET Core class library I wrote to work with CSV files, the source code can be found on GitHub. You can of course use any of your projects.
Open a Command Prompt in your project directory:
Run dotnet pack
command to create a NuGet package (by default this will also build the project):
1 |
dotnet pack -o publish -c Release --version-suffix alpha |
Options used are:
-o
is the path where package is created-c
specifies build configuration--version-suffix
specifies that this is a pre-release. You can omit this for your final release
You’ll have the NuGet package created in the publish folder:
Run dotnet nuget push
command to upload NuGet package to nuget.org:
1 |
dotnet nuget push Fiver.Lib.Csv.1.0.0-alpha.nupkg -s https://www.nuget.org -k <api-key> |
Options used are:
-s
specifies the URL to server where you’re uploading the package-k
specifies the API key that identifies you on the server
Browse to your NuGet website and you’ll find your package:
You could also specify other properties in .csproj
that adds metadata to your NuGet package, e.g.:
1 2 3 4 5 6 7 |
<PropertyGroup> <Authors>Tahir Naushad</Authors> <Title>CSV Reader/Writer Library</Title> <Description>This is a library that makes reading and writing CSV files easy</Description> <VersionPrefix>1.0.1</VersionPrefix> <VersionSuffix>beta</VersionSuffix> </PropertyGroup> |
Run dotnet pack
command again and open the package in NuGet Package Explorer to view the properites:
For more information on various properties you could set, please see details here.
Does this method of packaging leverage any of the package configurations on the project properties ‘Package’ tab?
Thanks for the question Allan. Yes it does. I’ve updated the post to mention this as others may benefit from this.