Skip to content

Commit c0c2df3

Browse files
authored
Update and refresh Create package dotnet (#2863)
1 parent bf0cc18 commit c0c2df3

File tree

3 files changed

+78
-75
lines changed

3 files changed

+78
-75
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,134 @@
11
---
2-
title: Create a NuGet package using the dotnet CLI
3-
description: A detailed guide to the process of designing and creating a NuGet package, including key decision points like files and versioning.
2+
title: Create a NuGet package with the dotnet CLI
3+
description: Read a detailed guide about the process of designing and creating a NuGet package, including key decision points like files and versioning.
44
author: JonDouglas
55
ms.author: jodou
6-
ms.date: 07/14/2022
6+
ms.date: 08/29/2022
77
ms.topic: conceptual
88
---
99

10-
# Create a NuGet package using the dotnet CLI
10+
# Create a NuGet package with the dotnet CLI
1111

12-
No matter what your package does or what code it contains, you use one of the CLI tools, either `nuget.exe` or `dotnet.exe`, to package that functionality into a component that can be shared with and used by any number of other developers. This article describes how to create a package using the dotnet CLI. To install the `dotnet` CLI, see [Install NuGet client tools](../install-nuget-client-tools.md). Starting in Visual Studio 2017, the dotnet CLI is included with .NET Core workloads.
12+
NuGet packages contain code that developers can reuse in their projects. No matter what your code does or contains, you use a command-line tool, either `nuget.exe` or `dotnet.exe`, to create the NuGet package.
1313

14-
For .NET Core and .NET Standard projects that use the [SDK-style format](../resources/check-project-format.md), and any other SDK-style projects, NuGet uses information in the project file directly to create a package. For step-by-step tutorials, see [Create .NET Standard Packages with dotnet CLI](../quickstart/create-and-publish-a-package-using-the-dotnet-cli.md) or [Create .NET Standard Packages with Visual Studio](../quickstart/create-and-publish-a-package-using-visual-studio.md).
14+
This article describes how to create a package by using the [dotnet CLI](). Starting in Visual Studio 2017, the dotnet CLI is included with all .NET and .NET Core workloads. If you need to install the dotnet CLI or other NuGet client tools, see [Install NuGet client tools](../install-nuget-client-tools.md).
1515

16-
`msbuild -t:pack` is functionality equivalent to `dotnet pack`. To build with MSBuild, see [Create a NuGet package using MSBuild](creating-a-package-msbuild.md).
16+
This topic applies only to .NET and other projects that use the [SDK-style format](../resources/check-project-format.md). For these projects, NuGet uses information from the project file to create a package. For quickstart tutorials, see [Create packages with the dotnet CLI](../quickstart/create-and-publish-a-package-using-the-dotnet-cli.md) or [Create packages with Visual Studio](../quickstart/create-and-publish-a-package-using-visual-studio.md).
1717

18-
> [!IMPORTANT]
19-
> This topic applies to [SDK-style](../resources/check-project-format.md) projects, typically .NET Core and .NET Standard projects.
20-
21-
## Set properties
18+
The MSBuild [msbuild -t:pack](creating-a-package-msbuild.md#run-the-msbuild--tpack-command) command is functionally equivalent to [dotnet pack](/dotnet/core/tools/dotnet-pack). For more information about creating a package with MSBuild, see [Create a NuGet package using MSBuild](creating-a-package-msbuild.md).
2219

23-
The following properties are required to create a package.
20+
> [!NOTE]
21+
> - To create and publish packages for non-SDK-style projects, typically .NET Framework projects, see [Create a package using the nuget.exe CLI](Creating-a-Package.md) or [Create and publish a package using Visual Studio (.NET Framework)](../quickstart/create-and-publish-a-package-using-visual-studio-net-framework.md).
22+
>
23+
> - For projects migrated from *packages.config* to [PackageReference](../consume-packages/package-references-in-project-files.md), use `msbuild -t:pack`. For more information, see [Create a package after migration](../consume-packages/migrate-packages-config-to-package-reference.md#create-a-package-after-migration).
2424
25-
- `PackageId`, the package identifier, which must be unique across the gallery that hosts the package. If not specified, the default value is `AssemblyName`.
26-
- `Version`, a specific version number in the form *Major.Minor.Patch[-Suffix]* where *-Suffix* identifies [pre-release versions](prerelease-packages.md). If not specified, the default value is 1.0.0.
27-
- The package title as it should appear on the host (like nuget.org)
28-
- `Authors`, author and owner information. If not specified, the default value is `AssemblyName`.
29-
- `Company`, your company name. If not specified, the default value is `AssemblyName`.
25+
## Set properties
3026

31-
In Visual Studio, you can set these values in the project properties (right-click the project in Solution Explorer, choose **Properties**, and select the **Package** tab). You can also set these properties directly in the project files (`.csproj`).
27+
You can create an example class library project by using the `dotnet new classlib` command, and package the project by using `dotnet pack`. The `dotnet pack` command uses the following properties. If you don't specify values in the project file, the command uses default values.
3228

33-
```xml
34-
<PropertyGroup>
35-
<PackageId>AppLogger</PackageId>
36-
<Version>1.0.0</Version>
37-
<Authors>your_name</Authors>
38-
<Company>your_company</Company>
39-
</PropertyGroup>
40-
```
29+
- `PackageId`, the package identifier, must be unique across nuget.org and any other targets that host the package. If you don't specify a value, the command uses the `AssemblyName`.
30+
- `Version` is a specific version number in the form `Major.Minor.Patch[-Suffix]`, where `-Suffix` identifies [prerelease versions](prerelease-packages.md). If not specified, the default value is `1.0.0`.
31+
- `Authors` are the authors of the package. If not specified, the default value is the `AssemblyName`.
32+
- `Company` is company information. If not specified, the default value is the `Authors` value.
33+
- `Product` is product information. If not specified, the default value is the `AssemblyName`.
4134

42-
> [!Important]
43-
> Give the package an identifier that's unique across nuget.org or whatever package source you're using.
35+
In Visual Studio, you can set these values in the project properties. Right-click the project in **Solution Explorer**, select **Properties**, and then select the **Package** section. You can also add the properties directly to the *.csproj* or other project file.
4436

45-
The following example shows a simple, complete project file with these properties included. (You can create a new default project using the `dotnet new classlib` command.)
37+
The following example shows a project file with package properties added.
4638

4739
```xml
4840
<Project Sdk="Microsoft.NET.Sdk">
4941
<PropertyGroup>
5042
<TargetFramework>netstandard2.0</TargetFramework>
51-
<PackageId>AppLogger</PackageId>
43+
<PackageId>UniqueID</PackageId>
5244
<Version>1.0.0</Version>
53-
<Authors>your_name</Authors>
54-
<Company>your_company</Company>
45+
<Authors>Author Name</Authors>
46+
<Company>Company Name</Company>
47+
<Product>Product Name</Product>
5548
</PropertyGroup>
5649
</Project>
5750
```
5851

59-
You can also set the optional properties, such as `Title`, `PackageDescription`, and `PackageTags`, as described in [MSBuild pack targets](../reference/msbuild-targets.md#pack-target), [Controlling dependency assets](../consume-packages/package-references-in-project-files.md#controlling-dependency-assets), and [NuGet metadata properties](/dotnet/core/tools/csproj#nuget-metadata-properties).
52+
You can add other optional properties, such as `Title`, `PackageDescription`, and `PackageTags`.
6053

61-
> [!NOTE]
62-
> For packages built for public consumption, pay special attention to the **PackageTags** property, as tags help others find your package and understand what it does.
54+
>[!NOTE]
55+
> For packages you build for public consumption, pay special attention to the `PackageTags` property. Tags help others find your package and understand what it does.
6356
64-
For details on declaring dependencies and specifying version numbers, see [Package references in project files](../consume-packages/package-references-in-project-files.md) and [Package versioning](../concepts/package-versioning.md). It is also possible to surface assets from dependencies directly in the package by using the `<IncludeAssets>` and `<ExcludeAssets>` attributes. For more information, see [Controlling dependency assets](../consume-packages/package-references-in-project-files.md#controlling-dependency-assets).
57+
The `dotnet pack` command automatically converts `PackageReference`s in your project files to dependencies in the created package. You can control which assets to include through the `IncludeAssets`, `ExcludeAssets` and `PrivateAssets` tags. For more information, see [Controlling dependency assets](../consume-packages/package-references-in-project-files.md#controlling-dependency-assets).
6558

66-
## Add an optional description field
59+
For more information about dependencies, optional properties, and versioning, see:
6760

68-
[!INCLUDE [add description to package](includes/add-description.md)]
61+
- [Package references in project files](../consume-packages/package-references-in-project-files.md)
62+
- [Package versioning](../concepts/package-versioning.md)
63+
- [NuGet metadata properties](/dotnet/core/tools/csproj#nuget-metadata-properties)
64+
- [MSBuild pack targets](../reference/msbuild-targets.md#pack-target)
6965

70-
## Choose a unique package identifier and set the version number
66+
### Choose a unique package identifier and set the version number
7167

7268
[!INCLUDE [choose-package-id](includes/choose-package-id.md)]
7369

70+
### Add an optional description field
71+
72+
[!INCLUDE [add description to package](includes/add-description.md)]
73+
7474
## Run the pack command
7575

76-
To build a NuGet package (a `.nupkg` file) from the project, run the `dotnet pack` command, which also builds the project automatically:
76+
To build the NuGet package or *.nupkg* file, run the [dotnet pack](/dotnet/core/tools/dotnet-pack) command from the project folder, which also builds the project automatically.
7777

7878
```dotnetcli
79-
# Uses the project file in the current folder by default
8079
dotnet pack
8180
```
8281

83-
The output shows the path to the `.nupkg` file.
82+
The output shows the path to the *.nupkg* file:
8483

8584
```output
86-
Microsoft (R) Build Engine version 15.5.180.51428 for .NET Core
87-
Copyright (C) Microsoft Corporation. All rights reserved.
88-
89-
Restore completed in 29.91 ms for D:\proj\AppLoggerNet\AppLogger\AppLogger.csproj.
90-
AppLogger -> D:\proj\AppLoggerNet\AppLogger\bin\Debug\netstandard2.0\AppLogger.dll
85+
MSBuild version 17.3.0+92e077650 for .NET
86+
Determining projects to restore...
87+
Restored D:\proj\AppLoggerNet\AppLogger\AppLogger.csproj (in 97 ms).
9188
Successfully created package 'D:\proj\AppLoggerNet\AppLogger\bin\Debug\AppLogger.1.0.0.nupkg'.
9289
```
9390

9491
### Automatically generate package on build
9592

96-
To automatically run `dotnet pack` when you run `dotnet build`, add the following line to your project file within `<PropertyGroup>`:
93+
To automatically run `dotnet pack` whenever you run `dotnet build`, add the following line to your project file in the `<PropertyGroup>` tag:
9794

9895
```xml
9996
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
10097
```
10198

102-
When you run `dotnet pack` on a solution, this packs all the projects in the solution that are packable ([\<IsPackable\>](/dotnet/core/tools/csproj#nuget-metadata-properties) property is set to `true`).
103-
10499
> [!NOTE]
105-
> When you automatically generate the package, the time to pack increases the build time for your project.
100+
> When you automatically generate the package, packing increases the build time for your project.
101+
102+
Running `dotnet pack` on a solution packs all the projects in the solution that are packable, that is, have the `IsPackable` property set to `true`.
106103

107104
### Test package installation
108105

109-
Before publishing a package, you typically want to test the process of installing a package into a project. The tests make sure that the necessary files all end up in their correct places in the project.
106+
Before you publish a package, you should test installing the package into a project. Testing ensures that the necessary files end up in their correct places in the project.
110107

111-
You can test installations manually in Visual Studio or on the command line using the normal [package installation steps](../consume-packages/overview-and-workflow.md#ways-to-install-a-nuget-package).
108+
Test the installation manually in Visual Studio or on the command line by using the normal [package installation process](../consume-packages/overview-and-workflow.md#ways-to-install-a-nuget-package).
112109

113110
> [!IMPORTANT]
114-
> Packages are immutable. If you correct a problem, change the contents of the package and pack again, when you retest you will still be using the old version of the package until you [clear your global packages](../consume-packages/managing-the-global-packages-and-cache-folders.md#clearing-local-folders) folder. This is especially relevant when testing packages that don't use a unique prerelease label on every build.
111+
> - You can't change packages once created. If you correct a problem, change the package contents and repack.
112+
>
113+
> - After you recreate the package, retesting still uses the old version of the package until you [clear your global packages folder](../consume-packages/managing-the-global-packages-and-cache-folders.md#clearing-local-folders). Clearing the folder is especially important for packages that don't use a unique prerelease label on every build.
114+
115+
## Next steps
115116

116-
## Next Steps
117+
Once you create the package, you can publish the *.nupkg* file to the host of your choice.
117118

118-
Once you've created a package, which is a `.nupkg` file, you can publish it to the gallery of your choice as described on [Publishing a Package](../nuget-org/publish-a-package.md).
119+
> [!div class="nextstepaction"]
120+
> [Publish a package](../nuget-org/publish-a-package.md)
119121
120-
You might also want to extend the capabilities of your package or otherwise support other scenarios as described in the following topics:
122+
See the following articles for ways to extend the capabilities of your package or support other scenarios:
121123

122124
- [Package versioning](../concepts/package-versioning.md)
123125
- [Support multiple target frameworks](../create-packages/multiple-target-frameworks-project-file.md)
124126
- [Add a package icon](../reference/nuspec.md#icon)
125127
- [Transformations of source and configuration files](../create-packages/source-and-config-file-transformations.md)
126128
- [Localization](../create-packages/creating-localized-packages.md)
127-
- [Pre-release versions](../create-packages/prerelease-packages.md)
129+
- [Prerelease versions](../create-packages/prerelease-packages.md)
128130
- [Set package type](../create-packages/set-package-type.md)
129131
- [MSBuild props and targets](../concepts/MSBuild-props-and-targets.md)
130132
- [Create packages with COM interop assemblies](../create-packages/author-packages-with-COM-interop-assemblies.md)
131-
132-
Finally, there are additional package types to be aware of:
133-
134-
- [Native Packages](../guides/native-packages.md)
135-
- [Symbol Packages](../create-packages/symbol-packages-snupkg.md)
133+
- [Create native packages](../guides/native-packages.md)
134+
- [Create symbol packages (.snupkg)](symbol-packages-snupkg.md)

docs/create-packages/includes/add-description.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
The package's optional description, displayed on the package's NuGet.org page, is either pulled in from the `<description></description>` used in the `.csproj` file or pulled in via the `$description` in the [.nuspec file](../../reference/nuspec.md).
1+
The package's optional description appears on the **README** tab of the package's nuget.org page. The description pulls from the `<Description>` in the project file or the `$description` in the [.nuspec file](../../reference/nuspec.md).
22

3-
An example of a _description_ field is shown in the following XML text of the `.csproj` file for a .NET package:
3+
The following example shows a `Description` in the *.csproj* file for a .NET package:
44

55
```xml
66
<Project Sdk="Microsoft.NET.Sdk">

0 commit comments

Comments
 (0)