使用nuget 打包并上传 nuget.org

一、 准备工作

1 下载  Download NuGet.exe 

2  windows 系统下设置环境变量 path中 或者 在dos 命令窗口下转到 nuget.exe 所在目录

3 在www.nuget.org 注册一个账号,并获取一个API Key。好完事具备,开始吹东方。

二、打包你的项目

1  项目写好后,编辑AssemblyInfo.cs  信息

image

// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("Dapper.DBContext")]
[assembly: AssemblyDescription("A lightweight ORM based on dapper")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("guochun")]
[assembly: AssemblyProduct("Dapper.DBContext")]
[assembly: AssemblyCopyright("Copyright © guochun 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。  如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("a27f661e-a520-49b1-b820-b2bdd04db8cb")]

// 程序集的版本信息由下面四个值组成: 
//
//      主版本
//      次版本 
//      生成号
//      修订号
//
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
// 方法是按如下所示使用“*”: 
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.0.0")]
[assembly: AssemblyFileVersion("0.1.0.0")]

编译好代码,准备开始打包。

2 点击开始,运行,输入cmd 打开命令窗口。 转到项目所在文件夹

执行打包命令:  nuget spec

image

会 提示成功创建 Dapper.Context.nuspec  文件

用文本编辑器打开这个文件。

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
    <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
    <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
   
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2016</copyright>
    <tags>Tag1 Tag2</tags>
  </metadata>
</package>

其中带$符号的就是变量,你懂的。 红色 标注的 <releaseNotes>节点如果没有,就删掉。修改后如下:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
    <projectUrl>https://github.com/iampkm/Dapper.DBContext</projectUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <copyright>Copyright 2016</copyright>
  </metadata>
</package>

3 生成上传包文件

在命令窗口中执行命令  : nuget pack Dapper.DBContext.csproj

也可以加上用release编译打包 : nuget pack Dapper.DBContext.csproj -Prop Configuration=Release

image

需要注意的是,你项目必须用release 方式编译,否则会出现这种错误:

image

此时,项目目录下,就已经生成好打包文件了。

image

接下来,只需要把这个文件上传到 nug.org服务器即可。

四  上传 nug.org 服务器

1  首先 要在本地设置 APIKEY。从你的nuget 账号里查找 APIKEY,然后执行下面的命令

nuget setApiKey Your-API-Key  -Source https://www.nuget.org/api/v2/package

红色部分替换为你自己的 API-key,  这个命令只需要第一次上传的时候执行,以后就不需要了,因为本机已经保存了。官网解释:This will store your API key so that you never need to do this step again on this machine.

2  最后一步,上传你的包文件

nuget push Dapper.DBContext.0.1.0.0.nupkg -Source https://www.nuget.org/api/v2/package

image

3 大功告成,在自己的nuget账号里,在 Manage My Packages 功能里,就能看到以上传的包了。

image

不过现在你并不能立即通过vs 的包管理器下载使用 ,官网提示:这个刚加入的还没编入索引,所以在vs 里暂时还不能通过搜索查到。

This package has not been indexed yet. It will appear in search results and will be available for install/restore after indexing is completed.

等了大概一分钟后,

image

nice~~~~~~~!

原文地址:https://www.cnblogs.com/iampkm/p/5740686.html