How to publish your own package to nuget.org using visual studio code with dotnet cli?
In this article, you will learn how to publish your own package to NuGet using visual studio code with dotnet cli command.
Before we go publishing your project into a package, The prerequisite is you should have a project.
Step 1: Create a class lib project
> dotnet new classlib — name applibpack
Step 2: Add a new class and method contains a console statement to display a message.
Step 3: For package information, Add the following properties in .csproj file
<PackageId>AppLibPack</PackageId>
<Version>1.0.0</Version>
<Authors>Nani</Authors>
<Company>Self</Company>
Step 4: Once all code changes, build the solution and run the package command
> dotnet build
> dotnet pack
This dotnet pack will generate a .nupkg file inside the bin debug folder.
Step 5: To publish this file to nuget.org, You must register and log in to the nuget.org website.
Step 6: To generate a key that is used to authenticate with NuGet from your project.
To generate a key, First, click on UserName => ApiKeys => Click on Create link.
There are few mandatory Input Fields,
KeyName: Give any name for the key
Glob Pattern: *
Where * means All packages
Finally, click on Create button this will give us an API key. (You will get this key when you click on copy link )
Step 7: To push your NuGet package file into nuget.org. First, navigate the directory where this .nupkg location in the command prompt
> dotnet nuget push <Your_Nuget_Package_File>— api-key <Your_NUGET_API_KEY>— source https://api.nuget.org/v3/index.json
Note: Replace <Your_Nuget_Package_File> with your package name and <Your_NUGET_API_KEY> with your generated key.
Once executed the message you will see in the console window is “ Your package pushed successfully”.
Step 8: To see your package in nuget.org, click on userName => Manage Packages. Initially, this will list inside the Unlisted package and after some time this will go into published lists.
Step 9: It’s time to consume your package in another project.
To add this package to your another project run the following command,
>dotnet add package AppPackLib — version 1.0.0
Step 10: Add this namespace into the class where you want to use and access the class and its method.
Official Reference link: https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-the-dotnet-cli
Thanks for being here and keep learning. Wear Mask and stay safe.