NuGet is a free and open source package management system for .NET applications which simplifies the process of incorporating third party libraries into the .NET applications. Earlier, developers used to add references to the third party libraries into their project which was a tedious task to do so as many libraries are dependent on other third party libraries. Nuget culminates all those difficult task and helps to install the package libraries in a single go. In this step-by-step article we will learn how to create our own NuGet package.
Introduction
NuGet is a free and open source package management system for .NET applications which simplifies the process of incorporating third party libraries into the .NET applications. Earlier, developers used to add references to the third party libraries into their project which was a tedious task to do so as many libraries are dependent on other third party libraries. NuGet culminates all those difficult task and helps to install the package libraries in a single go. In this step-by-step article we will learn how to create our own NuGet package.
Step 1 : Create a class library project
Open Visual Studio and create a class library project. Name it as NugetExperiment
Our class library project will be a simple Maths Calculator with the following implementation
namespace NugetExperiment
{
public class BasicMathsCalculator
{
/// <summary>
/// Function Name: BasicArithmeticOperation
/// Purpose : Serves the basic arithmetic operations
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="operator"></param>
/// <returns></returns>
public int BasicArithmeticOperation(int a , int b, string @operator)
{
return @operator == "/" ? (a / b) :
@operator == "*" ? (a * b) :
@operator == "-" ? (a - b) :
(a + b);
}
}
}
Build it and we will get NugetExperiment.dll
Step 2 : Create NugetSpec file
Download the nuget.exe from . Copy the nuget.exe file to say D:\Experiments\MyNuget
Open the Developer Command Prompt for VS2015 in elevated mode and execute nuget spec
The NuSpec file is an xml file that contains metadata of the package. It's extension is .nuspec
If we open Package.nuspec in an editor, it looks as under
<?xml version="1.0"?>
<package >
<metadata>
<id>Package</id>
<version>1.0.0</version>
<authors>A-niladri</authors>
<owners>A-niladri</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package description</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>Tag1 Tag2</tags>
<dependencies>
<dependency id="SampleDependency" version="1.0" />
</dependencies>
</metadata>
</package>
The complete reference of available tags can be read from NuGet website.
Now let's make the below changes in the Package.nuspecfile
<?xml version="1.0"?>
<package >
<metadata>
<id>Package</id>
<version>1.0.0</version>
<title>First Nuget Package Of RNA Team </title>
<authors>RNA Team</authors>
<owners>RNA Team</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A NugetPackage to demonstrate Basic Arithmetic Oprations</description>
<releaseNotes>RNA Team has Created the package</releaseNotes>
<copyright>Copyright RNATeam 2016</copyright>
<tags>BasicCalculator Add Subtract Multiply Divide</tags>
<dependencies>
<dependency id="SampleDependency" version="1.0" />
</dependencies>
</metadata>
</package>
For adding our NugetExperiment.dll, let's add the file tag as shown below
<?xml version="1.0"?>
<package >
<metadata>
<id>Package</id>
<version>1.0.0</version>
<title>First Nuget Package Of RNA Team </title>
<authors>RNA Team</authors>
<owners>RNA Team</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A NugetPackage to demonstrate Basic Arithmetic Oprations</description>
<releaseNotes>RNA Team has Created the package</releaseNotes>
<copyright>Copyright RNATeam 2016</copyright>
<tags>BasicCalculator Add Subtract Multiply Divide</tags>
<dependencies>
<dependency id="SampleDependency" version="1.0" />
</dependencies>
</metadata>
<files>
<file src="D:\Experiments\NugetExperiment\bin\Debug\NugetExperiment.dll" target="lib" />
</files>
</package>
Step 3 : Build the package
Open the Developer Command Prompt for VS 2015 in elevated mode and execute the command nuget pack Package.nuspec
Now let's visit the folder and our Package.1.0.0.nupkg file looks as under
Step 4 : Publish the package
We will publish our package locally. First of all, let's create a folder on the machine and name it as MyNuGetPkg. Copy the Package.1.0.0.nupkg file to this folder.
Now, open Visual Studio and locate Tools > Options menu item.
Locate Nuget Package Manager Options and select Package Sources.
Now, first click the Green add button (Step 1)
Provide a Name(say RNA First Package) and a Source path(say D:\Experiments\MyNuGetPkg) where to put the generated packages (Step 2).
Click on the Update button (Step 3)
Lastly, click on the OK button (Step 4)
Step 5 : Use the package in Console Application
Now open a console application. Click on References > Manage Nuget Packages...
We will find our package in the Package Source
We can test the same by clicking on the install button. Once installed, expand the reference menu and we can find our dll has been added
Now let us try to use our dll. As can be figure out, it is asking for the dependency to be added.
Finally write the below simple program
using NugetExperiment;
using System;
namespace TestNugetPackage
{
class Program
{
static void Main(string[] args)
{
var calciInstance = new BasicMathsCalculator();
Console.WriteLine("Product is {0} ", calciInstance.BasicArithmeticOperation(10, 20, "*"));
Console.Read();
}
}
}
Reference
What is NuGet?
Conclusion
In this article we have learnt how to create our own NuGet package in a step by step manner. Hope this will be useful. Thanks for reading.