NuGet的使用、部署、搭建私有服务
目录
- 前言
- 什么是NuGet?
- 为什么要使用NuGet
- NuGet的优点
- 使用
- Get-Help NuGet
- Install-Package
- Get-Package
- Uninstall-Package
- 制作NuGet库包
- 搭建NuGet服务器
- 上传NetGet库包
- 新增NuGet源
- 总结
前言
Nuget是一个.NET平台下的开源的项目,它是Visual Studio的扩展。在使用Visual Studio开发基于.NET Framework的应用时,Nuget能把在项目中添加、移除和更新引用的工作变得更加快捷方便。
为什么要使用NuGet
拿AsyncModule.NetMQ.dll举例,AsyncModule.NetMQ.dll依赖NetMQ.dll,而NetMQ.dll又依赖AsyncIO.dll。
目前我们需要数据库连接的地方我们需要引用AsyncModule.NetMQ.dll,我们可能会把它手工烤到我们需要的项目中,但是由于AsyncModule.NetMQ.dll需要依赖NetMQ.dll,因此我们还需要手工把NetMQ.dll拷到我们的项目中,同时由于NetMQ.dll需要依赖AsyncIO.dll,因此我们还需要手工把AsyncIO.dll拷到我们的项目中。依赖这当中就会有些问题,比如我们忘记拷了,或者我们拷的版本不是我们当前需要的,就会导致很多问题。
NuGet就可以让我们避免这个问题。若我们需要的库包已经导入到我们库包服务器中,那么我们只需要一条语句就可以引用该dll,同时NuGet会自动将其依赖包一起引用到我们的项目中,这完全是自动的。
使用
使用Get-Help NuGet命令查看帮助
PM> Get-Help nuget
TOPIC
about_NuGet
SHORT DESCRIPTION
Provides information about NuGet Package Manager commands.
LONG DESCRIPTION
This topic describes the NuGet Package Manager commands. NuGet is an integrated package
management tool for adding libraries and tools to .NET projects.
The following NuGet cmdlets are included.
Cmdlet Description
------------------ ----------------------------------------------
Get-Package Gets the set of installed packages. With -ListAvailable,
gets the set of packages available from the package source.
Install-Package Installs a package and its dependencies into the project.
Uninstall-Package Uninstalls a package. If other packages depend on this package,
the command will fail unless the –Force option is specified.
Update-Package Updates a package and its dependencies to a newer version.
Add-BindingRedirect Examines all assemblies within the output path for a project
and adds binding redirects to the application (or web)
configuration file where necessary.
Get-Project Returns a reference to the DTE (Development Tools Environment)
for the specified project. If none is specifed, returns the
default project selected in the Package Manager Console.
Open-PackagePage Open the browser pointing to ProjectUrl, LicenseUrl or
ReportAbuseUrl of the specified package.
Register-TabExpansion Registers a tab expansion for the parameters of a command.
SEE ALSO
Online documentation: http://go.microsoft.com/fwlink/?LinkID=206619
Get-Package
Install-Package
Uninstall-Package
Update-Package
Add-BindingRedirect
Get-Project
Open-PackagePage
Register-TabExpansion
Install-Package
使用Get-Package安装库包
PM> Get-Package
Id Version Description/Release Notes
-- ------- -------------------------
AsyncIO 0.1.26.0 AsyncIO
AsyncModule.NetMQ 1.1.0 基于NetMQ的异步Socket框架
NetMQ 4.0.0.1 A 100% native C# port of the lightweight high performance messaging library ZeroMQ
Uninstall-Package
若我们需要上传我们的dll到NuGet服务器中,首先需要让我们VS编译时能导出NuGet所支持的.nupkg文件
在解决方案上面右击找到Enable NuGet Package Restore点击开启功能
开启后我们需要手动在项目的.csproj文件中在PropertyGroup下加入以下节点
<BuildPackage>trueBuildPackage>
<RestorePackages>trueRestorePackages>
同时在Project节点内增加以下内容
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.ErrorText>
PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
Target>
再次编译项目就会自动编译出.nupkg文件。
如果是.Net Standard 项目直接在程序右键打包即可打包。
搭建NuGet服务器
编译出NuGet我们需要将包上传到NuGet服务器中,这样我们才能在VS中从NuGet服务器中下载下来。这里我使用NuGet Package Explorer工具进行上传,官方支持Win10商店和使用Chocolatey下载。
若需要上传到NuGet官方服务器中可以在NuGet官网上传,但是我们一般需要上传到指定NuGet服务器上,如我们自己的NuGet服务器。
选择第一项找到本地的.nupkg文件
左侧可以编译一下信息,
当上传了多个版本的dll,NuGet.Server会根据包Id和Version进行分组
在输入命令的时候可以用TAB键智能提示出当前所有版本号
我们也可用通过命令上传
nuget.exe push {package file} {apikey} -Source http://www.jnuget.com:10080/nuget
当我们同一个包上传过同一个版本的时候再次上传会报错,我们需要删除NuGet.Server已存在的包,后才能再次上传。或者我们可以允许通过包同一个版本允许覆盖上传,将web.Config的allowOverrideExistingPackageOnPush配置改为true即可
新增NuGet源
通过此片文章讲解了如何使用、部署NuGet,如何编译生成,上传库包到NuGet。
出处:https://www.cnblogs.com/Jack-Blog/p/7890369.html