Nuget总结

Nuget总结NuGet 是一个 NET 平台下的开源项目 是 NET 平台的包管理工具

大家好,欢迎来到IT知识分享网。

在这里插入图片描述

什么是Nuget

Nuget包的扩展名为.nupkg的单个ZIP文件,其中包含编译代码(dll)、与该代码相关的其他文件以及描述性清单(包含包版本号等信息)。

  • 代码相关的其他文件:NuGet包还可能包含其他与代码相关的文件。这可能包括文档、图像、配置文件、语言文件(用于本地化)或者其他任何与该程序集一起使用或需要的文件。这些额外的文件确保了包的完整性和一致性,并为用户提供了更多关于如何使用该包的信息。
  • 描述性清单(包含包版本号等信息):在NuGet包中,有一个特殊的清单文件(通常是XML格式),用于描述包的基本信息和元数据。这个清单文件包含了包的名称、版本号、描述、作者、版权信息等,这些信息对于用户了解和使用包非常重要。此外,清单还可以包含指向包中其他文件的引用,例如程序集的名称和版本,以及指向任何文档或额外文件的链接。

Nuget客户端工具

包括但不限于:

  • dotnet SDK:适用于 .NET Core 和 .NET Standard 库的 CLI 工具,以及任何 SDK 样式项目(例如面向 .NET Framework 的项目 )。 此 CLI 工具包含在 .NET Core SDK 中,并在所有平台上提供核心 NuGet 功能。 在 Visual Studio 2017 及更高版本中,dotnet CLI 会自动随任何 .NET Core 相关工作负载一起安装。
  • nuget.exe:适用于 .NET Framework 库和任何 非 SDK 样式项目的 CLI 工具,例如面向 .NET Standard 库的项目。 在 Mono 下运行时,此 CLI 工具在 Windows 上提供所有 NuGet 功能,以及 Mac 和 Linux 上的大多数功能。
  • IDE中的包管理器:程序包管理器 UI 和 程序包管理器 控制台(Windows 上的 PowerShell)。

如何管理Nuget包

使用IDE管理Nuget包

  • 查找安装
    在这里插入图片描述
    注意要安装的包的名称、版本以及package source。

  • 更新包
    在这里插入图片描述
  • 管理解决方案的包
    在这里插入图片描述
    至此,我们可以将Nuget包引入到项目中,包被下载到了一个nuget公共目录,例如Windows10系统上是 C:\Users\Username.nuget\packages,这样nuget包就不会被重复下载。而在项目中nuget仅仅将依赖信息写入了csproj项目文件与obj文件夹中的project.assets.json,其中csproj项目文件中的内容如下。

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Newtonsoft.Json" Version="11.0.2" /> </ItemGroup> </Project> 

实际Nuget包的引用场景:在这里插入图片描述
在此项目中,有三个不同的地方引用了B,而这三个引用可能对B包的版本有不同的要求。Nuget会自动管理所有包的依赖关系。对于被多次引用的包,Nuget会找到一个版本,以满足所有使用者的需求(但在某些情况下,由于版本冲突,可能无法找到合适的包)。

使用dotnet CLI管理

  • 安装特定版本的包
dotnet add package <PACKAGE_NAME> -v <VERSION> 
  • 删除包
dotnet remove package <PACKAGE_NAME> 

使用NuGet CLI 管理 NuGet 包

  • 安装包
    使用 -OutputDirectory 选项将包安装到特定目录。 如果未指定输出目录,install 会使用当前目录。
nuget install <packageID | configFilePath> -OutputDirectory <outputDirectory> 

可以指定当前或另一个目录中的现有 packages.config 文件,而不是指定要安装的包。 该 install 命令将安装 packages.config 文件中列出的所有包。

nuget install packages.config 
  • 安装特定版本的包
nuget install <packageID | configFilePath> -Version <version> 

注意:install 命令不会修改项目文件或 packages.config 文件。 install 和 restore 命令仅将包添加到磁盘,但不向项目添加依赖项。 若要添加项目依赖项,请通过 Visual Studio 包管理器 UI或包管理器控制台添加包。

  • 更新所有包
    使用 update 命令将项目 packages.config 文件中的所有包更新为其最新可用版本
nuget update 
  • 删除包
    若要删除包,请从项目文件夹中删除该包。
    从磁盘中删除包不会更新项目、packages.config 或 NuGet.Config 文件。 删除包的最佳方式是通过 IDE包管理器 UI 。

创建Nuget包

使用 dotnet CLI 创建 NuGet 包
设置属性:
配置项目文件(<项目名>.csproj)以指定包的元数据和依赖项。
例如,你可以添加版本号、描述、作者等元数据,以及任何其他依赖项。


<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <PackageId>YourPackageId</PackageId> <Version>1.0.0</Version> <Authors>YourName</Authors> <Description>Your package description</Description> </PropertyGroup> <ItemGroup> <PackageReference Include="YourPackageDependency" Version="1.0.0" /> </ItemGroup> </Project> 

运行创建命令:

dotnet pack -c Release 
nuget spec 

例如:

<?xml version="1.0"?> <package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata> <!-- Identifier that must be unique within the hosting gallery --> <id>Contoso.Utility.UsefulStuff</id> <!-- Package version number that is used when resolving dependencies --> <version>1.8.3</version> <!-- Authors contain text that appears directly on the gallery --> <authors>Dejana Tesic, Rajeev Dey</authors> <!-- Owners are typically nuget.org identities that allow gallery users to easily find other packages by the same owners. --> <owners>dejanatc, rjdey</owners> <!-- Project URL provides a link for the gallery --> <projectUrl>http://github.com/contoso/UsefulStuff</projectUrl> <!-- License information is displayed on the gallery --> <license type="expression">Apache-2.0</license> <!-- Icon is used in Visual Studio's package manager UI --> <icon>icon.png</icon> <!-- If true, this value prompts the user to accept the license when installing the package. --> <requireLicenseAcceptance>false</requireLicenseAcceptance> <!-- Any details about this particular release --> <releaseNotes>Bug fixes and performance improvements</releaseNotes> <!-- The description can be used in package manager UI. Note that the nuget.org gallery uses information you add in the portal. --> <description>Core utility functions for web applications</description> <!-- Copyright information --> <copyright>Copyright ©2016 Contoso Corporation</copyright> <!-- Tags appear in the gallery and can be used for tag searches --> <tags>web utility http json url parsing</tags> <!-- Dependencies are automatically installed when the package is installed --> <dependencies> <dependency id="Newtonsoft.Json" version="9.0" /> </dependencies> </metadata> <!-- A readme.txt to display when the package is installed --> <files> <file src="readme.txt" target="" /> <file src="icon.png" target="" /> </files> </package> 

运行创建命令:

nuget pack <清单名>.nuspec 

构建完成后,将在当前目录中生成一个NuGet包文件(.nupkg)。

发布Nuget包

发布到nuget.org
若要在 nuget.org 上发布,请使用 Microsoft 帐户登录 nuget.org,并使用它创建 nuget.org 帐户。
在这里插入图片描述
上传过程:
在这里插入图片描述
使用命令行推送
dotnet CLI:





dotnet nuget push <包名>.nupkg -s <源URL> -k <API密钥> 
nuget push YourPackageName.nupkg -s <源URL> -k <API密钥> 
  1. 登录你的 nuget.org 帐户,或创建一个帐户。
  2. 选择右上角的用户名,然后选择“API 密钥”。
  3. 选择“创建”,为秘钥提供一个名称。
  4. 在“选择范围”下,选择“推送”。
  5. 在“选择包>”“Glob 模式”下,输入 *。
  6. 选择创建。
  7. 选择“复制”以复制新秘钥。
    在这里插入图片描述

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/139795.html

(0)
上一篇 2025-06-01 21:26
下一篇 2025-06-01 21:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信