How to create a package in Nuget? | Nuget Tutorial


create-a-package-in-nugetHosting your own Repository in Nuget

How to create a package in Nuget?

Mehtod 1 – From An Assembly
If you have an assembly, you can easily generate a nuspec file from metadata within the assembly and create a package.

$ nuget spec MyAssembly.dll

This creates a Nuspec file. Edit the NuSpec file as needed and then

$ nuget pack MyAssembly.nuspec

Mehtod 2 – Commands Lines using convention based working directory
Some packages contain more than just assemblies. They may contain

Content and source code that should be injected into the target project.
PowerShell scripts and executables.
Configuration file and source code transformations.

To create a package in this way, you can layout a directory structure that follows the NuGet conventions.

tools – The tools folder of a package is for powershell scripts and programs accessible from the Package Manager Console. After the folder is copied to the target project, it is added to the `$env:Path (PATH) environment variable.
lib – Assemblies (.dll files) in the lib folder are added as assembly references when the package is installed.
content – Files in the content folder are copied to the root of your application when the package is installed.Think of the Content folder as the root of your target application. For example, if I want a package to add an image in the /images directory of the target application, make sure to place the image in the Content/images folder of the package.
build – The build folder of a package is for MSBuild targets files that are automatically inserted into the .csproj file of the application.

Step 1- Create the manifest

$ nuget spec

This will create an XML file with the .nuspec extension.

<?xml version="1.0"?>
   <package xmlns="">
   <description>Package description</description>
   <tags>Tag1 Tag2</tags>
   <dependency id="SampleDependency" version="1.0" />

Edit this file to fill in the values appropriate for your package. Then create the folders needed by your package and copy the correct content in each folder.

$ mkdir lib
   $ mkdir tools
   $ mkdir content
   $ mkdir content\controllers
   $ copy ..\src\SomeController.cs content
   $ copy ..\src\MyLibrary lib
 $ copy ..\src\SomePowershellScript.ps1 tool

With the working directory in place with content, run the following command:
nuget pack YourPackage.nuspec

Mehtod 3 – Using Visual Studio Projects
For simple packages, creating a package from a csproj or vbproj file is a convenient way to create packages. For example, other packages that have been installed into your project will be referenced as dependencies when you create a package this way.

In the folder where the csproj file is, run:

$ nuget spec

This creates a special nuspec file with tokens meant to be replaced at pack time based on the project metadata. For example, $version$ gets replaced by the version specified in the AssemblyVersionAttribute (or AssemblyInformationalVersionAttribute if present) applied to your assembly (typically within the AssemblyInfo.cs file).

These tokens will only be replaced when you execute the pack command with the project file, and not with the nuspec file. The contents of the nuspec file will be located and honored, with the appropriate token replacements executed.

The following is a list of the supported replacement tokens.

Token Source
$id$ The Assembly name
$version$ The assembly version as specified in the assembly’s
AssemblyVersionAttribute (or AssemblyInformationalVersionAttribute if present).
$author$ The company as specified in the AssemblyCompanyAttribute.
$description$ The description as specified in the AssemblyDescriptionAttribute.

You can then edit this nuspec file if you need to customize it. e.g. if you don’t want token replacement for some fields, you hard code them in the nuspec instead.

Also, if you want to pull additional files into the package, you can use a node in the nuspec. e.g. suppose you want to add all the files from some arbitrary other folder into the package, you’d have:

   <file src="..\..\SomeRoot\**\*.*" target="" /> 

You need to run ‘nuget pack’ on the project file, not the nuspec itself. But the nuspec will in fact get picked up. Once your nuspec is ready, you can run:

$ nuget pack MyProject.csproj

If the project references other projects, you can add the referenced projects as part of the package, or as dependencies with -IncludeReferencedProjects option. This is done recursively. For example, suppose you have project A.csproj, which references B.csproj and C.csproj, while B.csproj references D.csproj & E.csproj, C.csproj references F.csproj & G.csproj. Then, when you run

nuget pack A.csproj -IncludeReferencedProjects

the generated package will contain files from projects B, C, D, E, F & G, in addition to files from project A.

If a referenced project has a corresponding nuspec file with the same name, then that referenced project is added as a dependency instead. Using the same example, suppose now there is file C.nuspec in the same directory as project file C.csproj. When you run

$ nuget pack A.csproj -IncludeReferencedProjects

the generated package will contain files from projects B, D, E, in addition to files from project A, and the package has dependency on C.

By default, NuGet will use the default build configuration set in the project file (typically Debug). To pack files from a different build configuration (e.g., Release), you can run:

$ nuget pack MyProject.csproj -Prop Configuration=Release


Rajesh Kumar
Follow me
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x