您的位置:首页 > 其它

nuget.org 发布自己的程序包

2020-01-15 07:44 92 查看

安装 nuget

在 https://docs.microsoft.com/zh-cn/nuget/install-nuget-client-tools 选择 nuget.exe 安装。

下载下来是一个 nuget.exe 不可直接运行,随意放在一个目录,在环境变量 Path 中配置路径。

安装好了 CMD 中可以使用 >: nuget 命令结果是这样的:

C:\Users\bdzy>nuget
NuGet Version: 4.7.1.5393
usage: NuGet <command> [args] [options]
Type 'NuGet help <command>' for help on a specific command.

Available commands:

add         Adds the given package to a hierarchical source. http sources are not supported. For more info, goto https
://docs.nuget.org/consume/command-line-reference#add-command.

config      获取或设置 NuGet 配置值。

delete      从服务器中删除程序包。

help (?)    显示一般帮助信息,以及有关其他命令的帮助信息。

init        Adds all the packages from the <srcPackageSourcePath> to the hierarchical <destPackageSourcePath>. http fe
eds are not supported. For more info, goto https://docs.nuget.org/consume/command-line-reference#init-comm
and.

install     使用指定的源安装程序包。如果未指定源,则将使用 NuGet 配置文件中定义的所有源。如果配置文件未指定源,则使用默认的 NuGet 源。

list        显示给定源中的程序包列表。如果未指定源,则使用 %AppData%\NuGet\NuGet.config 中定义的所有源。如果 NuGet.config 未指定源,则使用默认 NuGet 源。

locals      Clears or lists local NuGet resources such as http requests cache, temp cache or machine-wide global packa
ges folder.

pack        基于指定的 nuspec 或项目文件创建 NuGet 程序包。

push        将程序包推送到服务器并进行发布。
通过加载 %AppData%\NuGet\NuGet.config,然后加载从驱动器的根目录开始到当前目录为止的任何 nuget.config 或 .nuget\nuget.config 来获取 NuGet 的默
认配置。

restore     还原 NuGet 程序包。

setApiKey   保存给定服务器 URL 所对应的 API 密钥。如果未提供 URL,则保存 NuGet 库的 API 密钥。

sign        Signs a NuGet package with the specified certificate.

sources     可以管理位于 %AppData%\NuGet\NuGet.config 的源列表

spec        为新程序包生成 nuspec。如果此命令在项目文件(.csproj、.vbproj、.fsproj)所在的文件夹中运行,则它将创建已标记化的 nuspec 文件。

update      将程序包更新到最新的可用版本。此命令还更新 NuGet.exe 本身。

verify      Verifies a signed NuGet package.

有关详细信息,请访问 http://docs.nuget.org/docs/reference/command-line-reference

 

本地制作 nuget 包(测试)

1、新建一个 类库项目 (.NET Framework),命名为 “Test01”

2、在 "Test01" 项目里面建 "Logger.cs" 类文件,里面随便写一个 log 方法

public class Logger
{
public void Log(string text)
{
Console.WriteLine(text);
}
}

3、重新生成项目(确保项目的 bin/debug 下面有生成项目的 dll )

 

生成项目清单(nuget.org 上所需的信息)

1、到项目的根目录下(含 .csproj 项目文件的目录)[以下操作均在项目目录下操作]

2、执行产生清单文件 nuspec 文件

nuget spec test01.csproj

会在目录里面产生一个 test01.nuspec 清单文件(xml 格式)

3、编辑 test01.nuspec 清单文件

<?xml version="1.0"?>
<package >
<metadata>
<id>[公司团队缩写].test01 (nuget 安装包的名称如:EntityFramework)</id>
<version>1.1</version> (版本使用 1.1,如果是兼容版本,使用 (1.0,2.0))
<title>$title</title>
<authors>$[作者]$</authors>
<owners>$author$</owners>
<licenseUrl>http://[开源协议的url地址]</licenseUrl>
<projectUrl>http://项目地址</projectUrl>
<iconUrl>http://图标地址(64*64)</iconUrl>
<requireLicenseAcceptance>false【bool: 是否提示同意协议使用】</requireLicenseAcceptance>
<description>$描述项目的信息$</description>
<releaseNotes>[备注项目信息和描述一样一样的]</releaseNotes>
<copyright>Copyright 2018</copyright>
<tags>Tag1 Tag2 [标签检索用的]</tags>
</metadata>
</package>

清单配置可选和必选项,必读 https://docs.microsoft.com/zh-cn/nuget/reference/nuspec#optional-metadata-elements

4、清单配置好了,保存文件

 

生成 .nupkg 文件

.nupkg 文件,就是用于发布到 nuget.org 的文件

nuget pack

NuGet 以 identifier-version.nupkg 形式生成 

.nupkg
 文件,你可在当前文件夹中找到该文件。

 

发布 nupkg 包到 nuget.org 

注意: nuget API KEY 申请在下面

nuget push test01.1.0.nupkg [nuget API KEY] -Source https://api.nuget.org/v3/index.json

nuget.exe 会显示发布过程的结果:

Pushing AppLogger.1.0.0.nupkg to 'https://www.nuget.org/api/v2/package'...
PUT https://www.nuget.org/api/v2/package/
Created https://www.nuget.org/api/v2/package/ 6829ms
Your package was pushed.

表示发布成功了。一般等待30分钟所有就可以用 清单文件配置的 ID [公司团队缩写].test01 在 nuget.org 搜索得到啦!

 

 

申请 nuget API KEY

  1. 登录你的 nuget.org 帐户,或创建一个帐户(如果你还没有帐户)。

  2. 选择用户名(在右上角),然后选择“API 密钥”。

  3. 选择“创建”,提供密钥名称,选择“选择范围”>“推送”。 在“API 密钥”下,输入“Glob 模式”*,然后选择“创建”。 (请参阅下面有关范围的详细信息。)

  4. 创建密钥后,选择“复制”,检索需要在 CLI 中使用的访问密钥:

 

转载于:https://my.oschina.net/TOW/blog/2873416

  • 点赞
  • 收藏
  • 分享
  • 文章举报
choulao8648 发布了0 篇原创文章 · 获赞 0 · 访问量 1034 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: