Publishing Your Project as a NuGet Package

2021/10/13
218 words
2 min

Publishing your project as nuget package can seems difficult for new developers, but it's really quite simple task. In this article I'll use my demo repo with simple progress bar for C#, which can be found on GitHub to guide you.

Practices

Let's start with few tips:

  • Establish unique package id
  • Use symver to version your package
  • Include descriptive package summary

Project configuration

Below you can find part of properly configured .csproj file.

xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
  <!-- ... -->

  <TargetFramework>net5.0</TargetFramework>
  <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  <GenerateDocumentationFile>true</GenerateDocumentationFile>

  <Product>SharpBar</Product>
  <Authors>Krzysztof Begiedza</Authors>
  <PackageLicenseExpression>MIT</PackageLicenseExpression>
  <Summary>Simple progress bar for your C# console app</Summary>
  <Copyright>Copyright © Krzysztof Begiedza 2021</Copyright>

  <Version>1.0.0</Version>
  <AssemblyVersion>1.0.0.0</AssemblyVersion>
  <FileVersion>1.0.0</FileVersion>
  <RepositoryUrl>https://github.com/kbegiedza/SharpBar.git</RepositoryUrl>

  <PackageId>SharpBar</PackageId>
  <PackageIcon>icon128.png</PackageIcon>
  <PackageTags>cli;progress</PackageTags>
  <PackageProjectUrl>https://sharpbar.kbegiedza.eu</PackageProjectUrl>

  </PropertyGroup>
</Project>

To add icon for package you can utilize <PackageIcon> tag with image included inside <ItemGroup> tag. NuGet gallery suggests icon with 128x128 px resolution encoded as .png file with transparent background.

xml
<ItemGroup>
    <None Include="../../docs/icon128.png" Pack="true" PackagePath="\"/>
</ItemGroup>

You can combine multiple values inside tags by using semicolon between tags, i.e.:

xml
<TargetFrameworks>netstandard2.1;net461</TargetFrameworks>

Publishing

Finally we can pack our package with dotnet pack -c Release and publish generated .nupkg file directly to NuGet repository. You can upload package manually, use automated pipeline (ex. GitHub Actions) or do it with bash/powerShell script using NuGet API keys.

To publish with CLI you can execute following code:

bash
dotnet nuget push <your-package>.nupkg --api-key <your-API-key> --source https://api.nuget.org/v3/index.json

Further reading

Using this knowledge you can go deep and experiment with:

  1. Signing
  2. NuGet API keys
  3. GitHub NugetPackages
  4. Package prefix registration

Last modified:


For commercial reprinting, please contact the webmaster for authorization, for non-commercial reprinting, please indicate the source of this article and the link to the article, you are free to copy and distribute the work in any media and in any form, and you can also modify and create, but the same license agreement must be used when distributing derivative works. This article is licensed under the CC BY-NC-SA 4.0 license.