用Nuget部署程序包

用Nuget部署程序包

 

Nuget是.NET程序包管理工具(类似linux下的npm等),程序员可直接用简单的命令行(或VS)下载包。好处:

(1)避免类库版本不一致带来的问题。GitHub是管理源代码的,这个是管理生成的类库/包的。

(2)可自动处理类库依赖:

(3)可指定下载历史版本: install-package name –version 1.0.2

(4)可部署私有包服务器: https://docs.microsoft.com/en-us/nuget/hosting-packages/overview

1.    注册账号

http://www.nuget.org

可用微软账户登录注册

2.    生成ApiKey

 

(1)       这个key最多只能用一年。超出了怎么办?不知道有没有续期措施。

(2)       Glob pattern:匹配模式,用户搜索时用的?

3.    发布包方案一:使用命令行

(1)       去官网下载Nuget.exe:

https://www.nuget.org/downloads

VS 2017已经内置了,但不知道怎么在VS内部运行nuget命令

(2)       在项目目录下运行命令:

nuget setApiKey xxxxxxxxx-5a3c-4a67-b969-3cda4f074341

nuget spec, 生成并编辑 .nuspec 文件(是包的配置信息)

nuget pack xxx.csproj, 生成 .nupkg 文件(包)

nuget push xxx.1.0.x.nupkg, 发布

4.    发布包方案二:使用NuGetPackageExplorer

参考:https://github.com/NuGetPackageExplorer/NuGetPackageExplorer

(1)安装并运行:Microsoft Store > NuGet Package Explorer

(2)新建包 > 添加文件夹/文件

 

可添加的目录类别包括:

build

输出到bin目录

content

原样拷贝

lib

添加到引用

src

源码拷贝

tools

(3)发布,填写入你的key

 

5.    安装测试

在vs>工具>Nuget包管理器>管理解决方案的Nuget程序包>浏览:

 

或者直接在程序包管理器控制台中用命令行

         Install-package packageName

 

部署后自动将引用、资源、配置等自动发布到新程序。

 

注意,包发布后有时延。

6.    其它

(1)     指定依赖的.NET类库版本

 

(2)     限制可下载的版本

 

(3)     引用类库

 

(4)     修改.config文件

在Content目录下包含app.config.transform 或 web.config.transform 文件。

https://docs.microsoft.com/zh-cn/nuget/create-packages/source-and-config-file-transformations

(5)     新增文件

在Content目录下放置代码文件,并附加.pp扩展名。如:

contentModelContosoData.cs.pp

代码中可用$token$放置一些变量(https://msdn.microsoft.com/library/vslangproj.projectproperties_properties.aspx

namespace $rootnamespace$.Models

{

    public struct CategoryInfo

    {

        public string categoryid;

        public string description;

        public string htmlUrl;

        public string rssUrl;

        public string title;

    }

}

(6)     部署到 bin 目录下

(7)     部署自己的Nuget服务器

https://github.com/NuGet/NuGetGallery#build-and-run-the-gallery-in-arbitrary-number-easy-steps

https://docs.microsoft.com/zh-cn/nuget/hosting-packages/nuget-server

 7. NetCore/NetStandard项目的Nuget包

这就更简单了,vs项目右键直接有打包选项,简化了很多,也可以直接编译时就生成包。

原文地址:https://www.cnblogs.com/surfsky/p/8072993.html