dotnet pack & Package Metadata
A NuGet package is a zip file with a .nupkg extension: the build outputs in lib/<tfm>/, a .nuspec manifest describing the package and its dependencies, and optional extras (README, icon, license, build files). dotnet pack builds the project and produces that archive from MSBuild properties, so the project file is the single source of truth for the package too.
A package with full metadata
mkdir greet && cd greet
cat > Greet.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>Ada.Greet</PackageId>
<Version>1.2.0</Version>
<Authors>Ada Lovelace</Authors>
<Description>Friendly greetings for console apps.</Description>
<PackageTags>greeting;console;demo</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
<PackageProjectUrl>https://example.com/greet</PackageProjectUrl>
<Copyright>Copyright 2026 Ada Lovelace</Copyright>
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="/" />
<None Include="icon.png" Pack="true" PackagePath="/" />
</ItemGroup>
</Project>
XML
echo 'namespace Ada.Greet; public static class Greeter { public static string Hello(string name) => quot;Hello, {name}!"; }' > Greeter.cs
printf '# Ada.Greet\n\nCall `Greeter.Hello("you")`.\n' > README.md
printf '\x89PNG\r\n\x1a\n' > icon.png
dotnet pack -o out -nologo
unzip -Z1 out/Ada.Greet.1.2.0.nupkg
unzip -p out/Ada.Greet.1.2.0.nupkg Ada.Greet.nuspec | tail -n +2; echo Determining projects to restore...
Restored /home/learner/greet/Greet.csproj (in 65 ms).
Greet -> /home/learner/greet/bin/Release/net10.0/Greet.dll
Successfully created package '/home/learner/greet/out/Ada.Greet.1.2.0.nupkg'.
_rels/.rels
Ada.Greet.nuspec
README.md
icon.png
lib/net10.0/Greet.dll
[Content_Types].xml
package/services/metadata/core-properties/nuget.psmdcp
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Ada.Greet</id>
<version>1.2.0</version>
<authors>Ada Lovelace</authors>
<license type="expression">MIT</license>
<licenseUrl>https://licenses.nuget.org/MIT</licenseUrl>
<icon>icon.png</icon>
<readme>README.md</readme>
<projectUrl>https://example.com/greet</projectUrl>
<description>Friendly greetings for console apps.</description>
<copyright>Copyright 2026 Ada Lovelace</copyright>
<tags>greeting console demo</tags>
<repository type="git" />
<dependencies>
<group targetFramework="net10.0" />
</dependencies>
</metadata>
</package>
What happened:
dotnet packbuilt Release (the default for pack since .NET 8) and wroteout/Ada.Greet.1.2.0.nupkg. Without-o, it would land inbin/Release/.- The assembly went to
lib/net10.0/. With multi-targeting there would be one folder per framework. - The
.nuspecwas generated from the properties:PackageId→id,Authors→authors,PackageTags(semicolons) →tags(spaces),PackageLicenseExpression→ alicenseelement with an SPDX expression (plus alicenseUrlfor very old clients). README.mdandicon.pnghad to be packed explicitly withPack="true" PackagePath="/"; the properties only point at them. nuget.org renders the README on the package page and shows the icon in search results._rels/,[Content_Types].xmland the.psmdcpfile come from the Open Packaging Conventions zip format NuGet is built on.
| Property | Notes |
|---|---|
PackageId |
defaults to the assembly name; choose a prefix you can reserve on nuget.org |
Version |
SemVer 2.0; also sets the assembly's informational version. Set it from CI with -p:Version=… |
PackageLicenseExpression / PackageLicenseFile |
an SPDX expression (MIT, Apache-2.0, MIT OR Apache-2.0) or a packed license file, not both |
PackageReadmeFile, PackageIcon |
paths inside the package; the files must be packed |
RepositoryUrl, PublishRepositoryUrl |
where the source lives; filled in automatically by SourceLink (next lesson) |
IsPackable |
false for apps and tests that should never become packages |
Dependencies come from the project
mkdir -p repo/Core repo/Text && cd repo
cat > Core/Core.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>Ada.Core</PackageId>
<Version>2.0.0</Version>
</PropertyGroup>
</Project>
XML
cat > Text/Text.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>Ada.Text</PackageId>
<Version>2.0.0</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Core/Core.csproj" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Humanizer.Core" Version="2.14.1" PrivateAssets="all" />
</ItemGroup>
</Project>
XML
echo 'namespace Ada; public class Core { }' > Core/Core.cs
echo 'namespace Ada; public class Text { }' > Text/Text.cs
dotnet pack Text -o out -nologo -v:q
ls out
unzip -p out/Ada.Text.2.0.0.nupkg Ada.Text.nuspec | sed -n '/<dependencies>/,/<\/dependencies>/p'Ada.Text.2.0.0.nupkg
<dependencies>
<group targetFramework="net10.0">
<dependency id="Ada.Core" version="2.0.0" exclude="Build,Analyzers" />
<dependency id="Newtonsoft.Json" version="13.0.3" exclude="Build,Analyzers" />
</group>
</dependencies>
- The
ProjectReferencebecame a package dependency onAda.Core2.0.0 (itsPackageIdandVersion), butout/holds onlyAda.Text: packing one project doesn't pack its references. A package can only depend on other packages, soAda.Coremust be packed and published too, typically by runningdotnet packon the whole solution. Newtonsoft.Jsonbecame a dependency with the range from the project:13.0.3means>= 13.0.3, so consumers may resolve a newer one.Humanizer.Coreis absent:PrivateAssets="all"keeps it out of the package's dependencies. IfAda.Textneeded it at run time, consumers would crash with a missing assembly. Use it only for build-time packages.exclude="Build,Analyzers"is NuGet's default for dependencies: a consumer gets your dependencies'libassets but not their build files and analyzers.
Pack validation
dotnet pack checks the metadata it can, and fails early on mistakes that would be embarrassing on nuget.org:
mkdir bad && cd bad
cat > bad.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
</Project>
XML
echo 'public class Bad { }' > Bad.cs
dotnet pack -nologo -v:q 2>&1 | grep -E 'error|warning' | awk '!seen[$0]++' | sed 's|/home/learner/bad/||'/usr/share/dotnet/sdk/10.0.402/NuGet.Build.Tasks.Pack.targets(226,5): error NU5039: The readme file 'README.md' does not exist in the package. [bad.csproj]
A README that the package doesn't contain is caught before anything is published (NU5039); a license file or icon that isn't packed fails the same way (NU5030, NU5046). To check what will be packed without building, dotnet pack --no-build reuses existing build outputs; in CI the usual sequence is dotnet build -c Release then dotnet pack -c Release --no-build.
Test yourself
PackageReadmeFile is set to README.md but the file isn't packed. What happens?
- nuget.org generates a README
- dotnet pack fails with an error about the missing readme file
- The README is downloaded from the repository
- Nothing
A packable project has a ProjectReference to another project. What does the package declare?
- Nothing; the referenced DLL is embedded
- A package dependency on the referenced project's PackageId and Version
- A dependency on the .csproj file
- An error: ProjectReferences can't be packed
Which PackageReference won't appear in the nuspec dependencies?
- One with Version="1.0"
- One with PrivateAssets="all"
- One with IncludeAssets="compile"
- One added by dotnet package add