How to create a NuGet package

How to create a NuGet package

A few days ago I looked into setting up our private NuGet server as a way for us to share code, instead of using git sub modules for example.
Turns out setting up the server is as simple as starting an empty web project in visual studio and adding the NuGet server package: http://nugetserver.net/.
On a side note, if you are already using team city, you can use this as a NuGet repository, just select create artifact with the package NuGet build step.

Getting started

So you have an awesome library you want to package and deploy so everyone can start using it. The first thing you want to do then is create a .nuspec file. Just add an empty text file or add a new XML file from visual studio. Use the same folder as your .csproj file and use the project name as a filename.

The file

An example file could be something like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
  <!-- more info: https://docs.microsoft.com/en-us/nuget/reference/nuspec -->
  <metadata>
    <id>MyLibrary</id>
    <version>$version$</version>
    <title>My awesome Library</title>
    <authors>E-Campus</authors>
    <owners>University of Antwerp</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <iconUrl></iconUrl>
    <tags></tags>
    <repository type="git" url="https://github.com/ShiveringSquirrel/Bootstrap-dotNET-WebControls" />
    <description>My awesome library, please use it everywhere.</description>
    <copyright>Copyright 2018 University of Antwerp</copyright>
    <dependencies>
      <group targetFramework=".NETFramework4.6">
        <dependency id="Newtonsoft.Json" version="11.0.2" />
        <dependency id="Microsoft.AspNet.WebApi.Client" version="5.2.6" />
      </group>
    </dependencies>
    <frameworkAssemblies>
      <frameworkAssembly assemblyName="System.Net.Http.Formatting" targetFramework=".NETFramework4.6" />
    </frameworkAssemblies>
  </metadata>
  <!--<files>
    <file src="bin\$configuration$\$id$.dll" target="lib\net46" />
  </files>-->
</package>

I’ve used some variables ($variable$) as an example:

  • The $version$ for instance will insert the assembly version and use that as the package version.
  • $configuration$ will contain Release, Debug, …
  • $id$ is the project id/name.

There are 3 important groups to note:

  • The dependencies group is used to list NuGet dependencies if any.
  • The frameworkAssemblies are .NET assemblies required for this project.
  • The files lists the files to package. This is commented out because we will let NuGet together with the project information decide what to package.

Lets get packaging

Make sure you have installed NuGet: https://www.nuget.org/downloads.
Open a command window in the project/nuspec folder (right click with shift pressed, open command window here).

nuget pack -IncludeReferencedProjects -properties Configuration=Release

Ship it

You have your package, lets upload it to the server:

nuget push .\MyLibrary.1.0.0.nupkg apiKey -Source https://nuget.myserver.be/nuget

That’s it.

Visual studio add feed

In visual studio you can add the NuGet server manually or you can include a config file in the root of your solution, to allow people who clone your project to have the new server(s) automatically configured.
An example nuget.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="MyServer" value="https://nuget.myserver.be/nuget" />
  </packageSources>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <bindingRedirects>
    <add key="skip" value="False" />
  </bindingRedirects>
  <packageManagement>
    <add key="format" value="0" />
    <add key="disabled" value="False" />
  </packageManagement>
</configuration>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.