In this article we will see how we can create a Nuget package for a class library and how to configure Visual Studio to have other package sources in addition to the default of nuget.org.
Let’s assume that we have a class library which we want to share as a Nuget package. In order to do so, we need to create a .nuspec
file in our project. The file can look like that:
<package> <metadata> <id>CommonFunctions.dll</id> <version>1.4.0</version> <title>CommonFunctions</title> <authors>Tasos K</authors> <owners>Tasos K</owners> <licenseUrl>http://www.codingstill.com</licenseUrl> <projectUrl>http://www.codingstill.com</projectUrl> <iconUrl>http://www.codingstill.com/path/to/icon</iconUrl> <requireLicenseAcceptance>false</requireLicenseAcceptance> <description>A collection of useful functions and classes for a web project</description> <releaseNotes>Image resize functionality</releaseNotes> <copyright>Copyright 2016</copyright> <tags>Tag 1, Tag 2</tags> <dependencies> <dependency id="BouncyCastle" version="1.8.1" /> </dependencies> </metadata> <files> <file src="bin\debug\CommonFunctions.dll" target="lib\NET452" /> <file src="bin\debug\CommonFunctions.pdb" target="lib\NET452" /> </files> </package>
The metadata contains some information about the package, who wrote it, the URL of the project etc. It is important to see the <dependencies>
tag where the package’s dependencies are defined. That means that whenever someone installs it through the Nuget package manager the listed packages will be also installed.
The <files>
tag also defines the files that we want to include in the package. We use a relative path (to the project root) for the src
attribute. The target
attribute defines the path where the files will be stored. We also need to mention the version where this library has been build. This way we can include in the same package binaries for different .NET versions.
After defining the .nuspec
, we need to build it. This process does not run with Visual Studio, yet we need to install the Nuget binaries. After installing (extracting actually) the binaries we can run the following command.
nuget pack C:\Projects\Path\To\CommonFunctions.csproj -Prop Configuration=Release -Prop Platform=AnyCPU -OutputDirectory C:\Packages\
This command will create the package and save it in the C:\Packages\
folder. It will create the CommonFunctions.dll.1.4.0.nupkg file.
Now, we can configure Visual Studio to see the C:\Packages\
folder as a package repository.
- In Visual Studio, go to Tools -> Nuget Package Manager -> Package Manager Settings
- In the window that opens, go to Package Sources
- There, click the plus icon, then set a name (e.g. My Packages and set C:\Packages\ as the source). You can also use a network path as source
- Click Update
After doing that, open the Nuget Package Manager and you will see in the Package source DropDown (top-right corner) the My Packages option. If you select that, you will see our newly created package!
This way you can share different versions of the same library across projects. Just remember to increment the version in the .nuspec
before running the command! 🙂
Leave a Reply