.NET Framework生成NuGet包

参考链接

这个教程适合于非SDK 样式项目:

  • 非 SDK 样式项目:通常为 .NET Framework 项目
  • SDK 样式项目:通常是 .NET Core 和 .NET Standard 项目,以及任何其他 SDK 样式项目

(一)下载nuget.exe工具并添加到环境变量

  • 将这个.exe安装包下载下来,放在适合的位置;下载地址
  • 将上述位置添加到环境变量中,具体添加的方法自行百度;

(二)什么是NuGet包

从本质上讲,NuGet包是ZIP文件,只不过是以.nupkg作为扩展名;

(三)确定要打包的程序集

  • 通常情况下,最好每个NuGet包有一个程序集,当然前提是每个程序集可以独立运行:
    • 例如,如果有一个 Utilities.dll 依赖于 Parser.dllParser.dll 可独立正常运行,则为每一个创建一个包。 这样做使得开发人员能够独立于 Utilities.dll 使用 Parser.dll
  • 如果库由多个不能独立运行的程序集构成,则可以合并到一个包中:
    • 使用上面的示例,如果 Parser.dll 包含仅由 Utilities.dll 使用的代码,那么可以将 Parser.dll 保留在同一包中。

(四).nuspec文件的角色和结构【这是说明步骤,可以跳过】

一旦确定需要打包的文件以后,下一步是在.nuspecXML文件中创建包清单

  • 清单:

    1. 介绍包的内容和自己是不是包含在包中;

    2. 驱动包的创建,并且指示NuGet如何将包安装到项目;

      ​ 例如,清单识别其他包依赖项,这样,NuGet 还可以在安装主包时安装这些依赖项。

    3. 包含所需属性和可选属性【如下所示】

  • 所需属性:

    1. 包标识符:在承载包的库中必须是唯一的;
    2. 窗体Major.Minor.Patch[-Suffix]中特定的版本号;其中-Suffix标识预发布版本;
    3. 包标题:出现在主机上;
    4. 创建者和所有者信息;
    5. 对包的详细说明;
  • 常见可选属性:

    1. 发行说明
    2. 版权信息
    3. Visual Studio 中的包管理器 UI 的简短说明
    4. 区域设置ID
    5. 项目URL
    6. 图标URL
    7. 依赖项和引用的列表
    8. 协助库进行搜索的标记
  • 示例:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
    <metadata>
        <!-- The identifier that must be unique within the hosting gallery -->
        <id>Contoso.Utility.UsefulStuff</id>

        <!-- The package version number that is used when resolving dependencies -->
        <version>1.8.3-beta</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>
        

        <!-- The icon is used in Visual Studio's package manager UI -->
        <iconUrl>http://github.com/contoso/UsefulStuff/nuget_icon.png</iconUrl>

        <!-- 
            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="" />
    </files>
</package>

(五)创建.nuspec文件

  • 创建完整的清单,通常使用下面的其中一种方法生成的基础.nuspec文件开始:

    1. 基于约定的工作目录;

    2. 程序集DLL;

    3. Visual Studio 项目;

    4. 包含默认值的新文件;

  • 之后手动编辑文件【以上面生成的基础文件为基础】

  • 生成的基础的nuspec文件包含占位符,在使用nuget pack命令之前,必须将其中的占位符修改;【即上面的手动修改的过程】

<1> 基于约定的工作目录
  • nupkg文件实际上是修改了扩展名的ZIP文件;
  • 可以直接在本地创建所需文件夹结构【因为文件夹结构是有规范的】,从这个结构生成.nuspec文件;
  • 之后使用nuget pack命令,自动将所有文件添加到该文件夹结构【不包含.开头的文件】;
  • 使用这种方法,可以不在清单中指定需要包含在包中的文件;
  • 文件夹的约定:

  • 文件夹结构可以包含任意数量的目标框架的任意数量的程序集,所以这种方法在创建支持多个框架的包时是必要的;

当准备好所需的文件夹结构之后,则在该文件夹中运行下面的命令,用来创建.nuspec文件:

nuget spec
<2>程序集DLL

使用下面的命令从程序集的元数据生成.nuspec文件:

nuget spec <assembly-name>.dll
  • 这个命令可以将生成的.nuspec文件中的一些占位符修改,如<id>, <version>,但是还包含其他属性的占位符;
<3> Visual Studio 项目

.csproj或者.vbproj文件创建.nuspec文件较为方便;这是因为其他已经安装到这些项目的包将会自动引用为依赖项 ;

  • 在含有<project-name>.csproj或者<project-name>.vbproj文件的文件夹中使用:
nuget spec

这样,生成的.nuspec文件中包含对所有其他已经安装的包的引用

  • 如果想要将程序依赖项包含在.nuspec中,在上述文件夹中使用:
nuget pack <project-name>.csproj
<4>包含默认值的新文件【略】

这种方法创建默认清单,可以确保一开始就使用正确的文件结构

(六)选择唯一的包标识符,并设置版本号

包标识符(<id>元素)和版本号(<version>元素)是清单中最重要的两个值,因为它们唯一标识包中包含的确切代码;

  • 包标识符:
    • 唯一性:标识符必须在 nuget.org 或托管包的任意库中是唯一的;
    • 类似于命名空间的名称:遵循类似于 .NET 中命名空间的模式,使用点表示法(而不是连字符)

(七)运行nuget包生成.nupkg文件

  • 当使用程序集或者基于约定的工作目录时,通过使用.nuspec文件运行nuget pack命令创建包:
nuget pack <project-name>.nuspec
  • 当使用Visual Studio项目时,使用项目文件运行nuget pack
nuget pack <project-name>.csproject

(八)如果以上生成的nuget包不能正常使用,可以使用下面的方法

直接从项目的.csproj文件中使用下面的命令:

nuget pack <project-name>.csproj -Build -Properties Configuration=Release
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值