Building NuGet Packages with the .NET CLI

Introduction

NuGet is a popular package manager for .NET that simplifies the process of incorporating third-party libraries into your projects. It also allows developers to create and share their own libraries, enhancing code reusability and collaboration. This article will guide you through building and publishing a NuGet package using the .NET CLI.

Prerequisites

Before we begin, ensure you have the following installed on your machine.

  • .NET SDK: Download and install from the official .NET website.
  • NuGet CLI: This is typically included with the .NET SDK, but you can download it separately from the NuGet website if needed.

Step 1. Create a Class Library

First, we need to create a class library project. Open your terminal or command prompt and navigate to the directory where you want to create your project. Run the following command.

dotnet new classlib -n MyLibrary

This command creates a new class library project named MyLibrary.

Step 2. Implement Your Library

Navigate to the project directory.

cd MyLibrary

Open the Class1.cs file and implement your desired functionality. For demonstration purposes, let's create a simple utility class.

public class Utility
{
    public static string GetGreeting(string name)
    {
        return $"Hello, {name}!";
    }
}

Step 3. Update the Project File

Next, we need to update the .csproj file to include metadata for the NuGet package. Open MyLibrary.csproj and add the following properties to the <PropertyGroup> tag.

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <PackageId>MyLibrary</PackageId>
    <Version>1.0.0</Version>
    <Authors>YourName</Authors>
    <Company>YourCompany</Company>
    <PackageDescription>A simple utility library for greeting users.</PackageDescription>
    <PackageTags>greeting;utility</PackageTags>
    <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

These properties provide essential information about your package, such as the package ID, version, authors, and description.

Step 4. Build the Package

With the project file updated, you can now build the NuGet package. Run the following command.

dotnet pack -c Release

This command builds the project and creates a NuGet package in the bin/Release directory. The package file will have a .nupkg extension.

Step 5. Publish the Package

To publish your package to NuGet.org, you need to create an API key. Follow these steps.

  1. Log in to NuGet.org and navigate to your account settings.
  2. Under the API keys section, create a new key with the desired permissions.
  3. Copy the generated API key.

With the API key ready, run the following command to publish your package.

dotnet nuget push bin/Release/MyLibrary.1.0.0.nupkg -k YOUR_API_KEY -s https://api.nuget.org/v3/index.json

Replace YOUR_API_KEY with the API key you generated earlier. This command pushes your package to NuGet.org, making it available for others to download and use.

Conclusion

Congratulations! You have successfully created and published a NuGet package using the .NET CLI. This process enables you to share your libraries with the community, promoting code reuse and collaboration. With your package now available on NuGet.org, developers around the world can easily integrate it into their projects.

Happy coding!