|
1 | 1 | ---
|
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. |
4 | 4 | author: JonDouglas
|
5 | 5 | ms.author: jodou
|
6 |
| -ms.date: 07/14/2022 |
| 6 | +ms.date: 08/29/2022 |
7 | 7 | ms.topic: conceptual
|
8 | 8 | ---
|
9 | 9 |
|
10 |
| -# Create a NuGet package using the dotnet CLI |
| 10 | +# Create a NuGet package with the dotnet CLI |
11 | 11 |
|
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. |
13 | 13 |
|
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). |
15 | 15 |
|
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). |
17 | 17 |
|
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). |
22 | 19 |
|
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). |
24 | 24 |
|
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 |
30 | 26 |
|
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. |
32 | 28 |
|
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`. |
41 | 34 |
|
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. |
44 | 36 |
|
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. |
46 | 38 |
|
47 | 39 | ```xml
|
48 | 40 | <Project Sdk="Microsoft.NET.Sdk">
|
49 | 41 | <PropertyGroup>
|
50 | 42 | <TargetFramework>netstandard2.0</TargetFramework>
|
51 |
| - <PackageId>AppLogger</PackageId> |
| 43 | + <PackageId>UniqueID</PackageId> |
52 | 44 | <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> |
55 | 48 | </PropertyGroup>
|
56 | 49 | </Project>
|
57 | 50 | ```
|
58 | 51 |
|
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`. |
60 | 53 |
|
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. |
63 | 56 |
|
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). |
65 | 58 |
|
66 |
| -## Add an optional description field |
| 59 | +For more information about dependencies, optional properties, and versioning, see: |
67 | 60 |
|
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) |
69 | 65 |
|
70 |
| -## Choose a unique package identifier and set the version number |
| 66 | +### Choose a unique package identifier and set the version number |
71 | 67 |
|
72 | 68 | [!INCLUDE [choose-package-id](includes/choose-package-id.md)]
|
73 | 69 |
|
| 70 | +### Add an optional description field |
| 71 | + |
| 72 | +[!INCLUDE [add description to package](includes/add-description.md)] |
| 73 | + |
74 | 74 | ## Run the pack command
|
75 | 75 |
|
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. |
77 | 77 |
|
78 | 78 | ```dotnetcli
|
79 |
| -# Uses the project file in the current folder by default |
80 | 79 | dotnet pack
|
81 | 80 | ```
|
82 | 81 |
|
83 |
| -The output shows the path to the `.nupkg` file. |
| 82 | +The output shows the path to the *.nupkg* file: |
84 | 83 |
|
85 | 84 | ```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). |
91 | 88 | Successfully created package 'D:\proj\AppLoggerNet\AppLogger\bin\Debug\AppLogger.1.0.0.nupkg'.
|
92 | 89 | ```
|
93 | 90 |
|
94 | 91 | ### Automatically generate package on build
|
95 | 92 |
|
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: |
97 | 94 |
|
98 | 95 | ```xml
|
99 | 96 | <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
100 | 97 | ```
|
101 | 98 |
|
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 |
| - |
104 | 99 | > [!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`. |
106 | 103 |
|
107 | 104 | ### Test package installation
|
108 | 105 |
|
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. |
110 | 107 |
|
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). |
112 | 109 |
|
113 | 110 | > [!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 |
115 | 116 |
|
116 |
| -## Next Steps |
| 117 | +Once you create the package, you can publish the *.nupkg* file to the host of your choice. |
117 | 118 |
|
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) |
119 | 121 |
|
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: |
121 | 123 |
|
122 | 124 | - [Package versioning](../concepts/package-versioning.md)
|
123 | 125 | - [Support multiple target frameworks](../create-packages/multiple-target-frameworks-project-file.md)
|
124 | 126 | - [Add a package icon](../reference/nuspec.md#icon)
|
125 | 127 | - [Transformations of source and configuration files](../create-packages/source-and-config-file-transformations.md)
|
126 | 128 | - [Localization](../create-packages/creating-localized-packages.md)
|
127 |
| -- [Pre-release versions](../create-packages/prerelease-packages.md) |
| 129 | +- [Prerelease versions](../create-packages/prerelease-packages.md) |
128 | 130 | - [Set package type](../create-packages/set-package-type.md)
|
129 | 131 | - [MSBuild props and targets](../concepts/MSBuild-props-and-targets.md)
|
130 | 132 | - [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) |
0 commit comments