Create a Nuget Package

Creating a NuGet Package

First, we need to understand the format of a .nuspec file. This is the main component of a NuGet package; it’s the manifest that explains where everything in the package resides and where it should be placed during installation. It’s fairly logical and is in standard XML format.

<?xml version="1.0" encoding="utf-8" ?>
<package>
  <metadata>
    <id>Nuget.Package.Name</id>
    <version>1.0.0</version>
    <title>Nuget Package Name</title>
    <authors>Company Ltd.</authors>
    <owners>Ravindra Naik</owners>
    <licenseUrl>http://ravindranaik.com</licenseUrl>
    <projectUrl>http://ravindranaik.com</projectUrl>
    <iconUrl>http://ravindranaik.com/logo.png</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>A description of your package</description>
    <releaseNotes>Initial 1.0.0 release</releaseNotes>
    <copyright>Copyright 2019 CompanyLtd</copyright>
    <tags>sample nuget</tags>
    <summary> A summary of your package </summary>
  </metadata>
  <files>
    <file src="readme.txt" />
    <file src="transforms\web.config.install.xdt" target="content\web.config.install.xdt" />
    <file src="transforms\views.web.config.install.xdt" target="content\Views\web.config.install.xdt" />
    <file src="App_Data\Models\**" target="content\App_Data\Models" />
    <file src="Controllers\**" target="content\Controllers" />
    <file src="Models\**" target="content\Models" />
    <file src="Views\**" target="content\Views" />
    <file src="Helpers\**" target="content\Helpers" />
    <file src="Content\**" target="content\Content" />
  </files>
</package>

The id must be unique if the package is to be hosted on Nuget.org but that’s not the aim of our project, so make it what you like.

The version is crucial to how Visual Studio Nuget Package Manager will interpret updates to the package. Start with 1.0.0 and increment in the major/minor/patch structure. Once a package is installed in a project, Package Manager will keep track of changes and show an upgrade icon if it detects a new version available on the NuGet server (local or remote).

The title and authors fields are obvious, though with authors you can match these to existing Nuget.org authors to form a link that can be navigated.

Pre – requisites:

  1. Install Nuget CLI. This is required so you can create the package from command line.
  2. Install Nuget Package Manager. This is a useful tool to inspect a package after it is created.

Create a Project

  1. Create a new blank solution in Visual Studio
  2. In the root of the solution add a blank text file. This will be your nuspec file. Name it the same as the name you will give your Nuget package e.g. “Nuget.Package.Name.nuspec”. Copy the template from the top of this post, or go to the source.
  3. Add the content and lib files to the project you want included. You can add references as usual that are required for the project to build, but they won’t be added to the package itself. Add any config transforms.
  4. Build the project

Create a Package

  1. In command line or PowerShell, navigate to your project directory.
  2. Run: nuget pack Nuget.Package.Name.nuspec. If all goes well you should now have a generated .nupkg file
  3. Open the generated .nupkg file in Nuget Package Manager and see if it looks correct.

Installing the Package

  1. Choose a folder on your computer or a network share drive to act as your local Nuget repository. A network drive will make it easy for other developers to use the repository.
  2. Copy your created Package to this folder
  3. In Visual Studio go to the Tools menu > Options.
    1. Open the Nuget Package Manager node
    2. Add a new Repository to a local or network path
    3. Ensure the new repository is ticked active. You only need to set this up once

  4. Create a new Project (e.g. MVC Project)
  5. Go to Nuget Package Manager > Manage Packages for this Solution and select your new local repository
  6. Click the Browse tab. Your new package should be available to install.
  7. Install the package. The console should show any dependencies you specified in the .nuspec being installed first, and you will need to agree to their terms and conditions as usual. Once finished, your readme.txt (if you have one) will load.

That’s it. Nuget package is created, now you can experiment your bit to upgrade the package with nuspec file.

Thanks for dropping by !!! Feel free to comment to this post or you can drop me an email at naik899@gmail.com.

Leave a Reply