MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

How to Create NuGet Packages with nuget.exe (Assembly, .nuspec, and .csproj Methods)

Creating a NuGet package is a core skill for .NET and DevOps engineers who need to distribute internal libraries, scripts, or reusable project assets. This guide explains the three packaging approaches shown in the original tutorial, when each one fits best, and the exact commands and manifest structure you need to produce a .nupkg artifact.

Originally published on 2015-11-21; fully revised and updated.

create-a-package-in-nuget

Choose the right packaging method

The original tutorial presents three ways to create packages with nuget.exe. In practice, your starting point (compiled DLL, folder layout, or project file) determines which workflow is fastest.

MethodBest forPrimary command pathNotes
From an assemblyYou already have a compiled DLL and want a quick packagenuget spec MyAssembly.dll then nuget pack MyAssembly.nuspecSimple and fast; metadata comes from assembly and edited .nuspec.
From a convention-based folder + nuspecPackages that include content, scripts, tools, or build assetsnuget spec then nuget pack YourPackage.nuspecMost flexible for non-DLL assets and custom folder structures.
From a Visual Studio projectProject-centric packaging with token replacement and optional referenced projectsnuget spec then nuget pack MyProject.csprojUses project metadata tokens like $id$ and $version$.

NuGet packaging workflow

All three methods follow the same high-level flow: generate or prepare a manifest, verify package content, build the package, and then publish to your feed.

flowchart TD
    A[Start packaging] --> B{What do you have?}
    B --> C[Compiled assembly]
    B --> D[Folder layout and files]
    B --> E[Project file]
    C --> F[Generate nuspec from dll]
    D --> G[Generate or edit nuspec]
    E --> H[Generate tokenized nuspec]
    F --> I[Edit metadata and dependencies]
    G --> I
    H --> I
    I --> J[Run nuget pack]
    J --> K[Validate nupkg contents]
    K --> L[Publish to repository]

Method 1: Create a package from an existing assembly

Use this when you already have a compiled assembly and want NuGet to scaffold a .nuspec from assembly metadata.

nuget spec MyAssembly.dll
nuget pack MyAssembly.nuspecCode language: CSS (css)

After generating the .nuspec, update metadata fields (ID, version, description, dependencies) before packing.

Method 2: Create a package from a convention-based directory and nuspec

This method is ideal when the package includes more than assemblies, such as content files, scripts, and build artifacts.

FolderPurpose in package
libAssemblies to be added as references on install.
toolsPowerShell scripts and executables accessible from Package Manager Console.
contentFiles copied into the target application structure.
buildMSBuild targets files applied to consuming projects.

Step 1: Generate the manifest

nuget spec

This creates a .nuspec file you can edit. Example manifest from the original tutorial:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>MyPackageId</id>
    <version>1.0</version>
    <authors>philha</authors>
    <owners>philha</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>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <dependency id="SampleDependency" version="1.0" />
    </dependencies>
  </metadata>
</package>Code language: HTML, XML (xml)

Step 2: Lay out package content and build

mkdir lib
mkdir tools
mkdir content
mkdir content\controllers
copy ..\src\SomeController.cs content\controllers
copy ..\src\MyLibrary.dll lib
copy ..\src\SomePowershellScript.ps1 tools
nuget pack YourPackage.nuspecCode language: CSS (css)

Method 3: Create a package from a Visual Studio project

For simple project-based packaging, run commands in the folder containing the .csproj (or .vbproj) file. NuGet creates a tokenized .nuspec and replaces tokens during packing when you pack the project file.

nuget spec
nuget pack MyProject.csprojCode language: CSS (css)

Supported replacement tokens highlighted in the original article:

TokenSource
$id$Assembly name
$version$Assembly version from assembly attributes
$author$Company from assembly attributes
$description$Description from assembly attributes

You can also include additional files through a <files> section in the .nuspec:

<files>
  <file src="..\..\SomeRoot\**\*.*" target="" />
</files>Code language: HTML, XML (xml)

Referenced projects can be included recursively with -IncludeReferencedProjects, and build configuration can be selected with -Prop:

nuget pack A.csproj -IncludeReferencedProjects
nuget pack MyProject.csproj -Prop Configuration=Release

Official references

Common pitfalls

  • Packing the .nuspec instead of the project file when you expect token replacement from project metadata.
  • Using a folder layout that does not match NuGet conventions, causing files not to land where consumers expect.
  • Forgetting to review dependency versions in the generated .nuspec before publishing.
  • Mixing Debug and Release artifacts unintentionally; use -Prop Configuration=Release when needed.
  • Assuming package creation and repository hosting are the same step; package creation is separate from publishing to a feed.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I'm Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms. I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.

Related Posts

Host Your Own NuGet Feed: Local Source vs NuGet.Server vs NuGetGallery

Learn how to host a private NuGet feed using three practical approaches: a local folder source, NuGet.Server, and NuGetGallery. This revised guide includes setup steps, command examples, and a workflow diagram to help you choose the right model.

Read More

Publish NuGet Packages to NuGet.org: CLI Steps, Workflow, and CI Example

Learn how to publish a NuGet package to NuGet.org using either nuget.exe or the dotnet CLI. This revised guide includes a workflow diagram, command comparison, and a CI-friendly example.

Read More

Public Package Repositories by Programming Language

Public Package Repositories by Programming Language Public package repositories (also known as package registries, artifact repositories or dependency repositories) allow developers to publish, discover, download and manage…

Read More

Team Foundation Build 2010: Architecture, Build Definitions, and Migration from TFS 2008

Team Foundation Build 2010 introduced a controller/agent build architecture, richer build definitions, and gated check-in workflows for TFS teams. This revised guide turns the original topic list into a practical reference for setup, customization, and migration planning.

Read More

.NET Build and Release Training: Curriculum, Toolchain, Workflow, and Enrollment

A structured, engineer-friendly rewrite of the original .NET Build and Release course page. Review objectives, tool coverage, CI/CD workflow, prerequisites, fees, and key links in one place.

Read More

NAnt Training Guide for Build and Release Engineers: Syllabus, Labs, and Workflow

A revised NAnt training page focused on practical skills for SCM, build, and release engineers. It includes the original syllabus topics, delivery options, a CI workflow diagram, and hands-on NAnt examples.

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
1 month ago

Creating a NuGet package is easier when you plan its structure and metadata before packing it. Use clear package names, meaningful version numbers, and accurate dependency details to help other developers use it safely. Testing the package in a clean project can catch missing files or references early. It is also worth adding a README with setup steps and examples. These small checks make a package easier to share, update, and maintain.

1
0
Would love your thoughts, please comment.x
()
x