團隊開發要共享 .NET 元件,一個標準的方式就把類別庫專案打包成 NuGet 套件,再發佈到公開 nuget.org 或內部私有 NuGet 伺服器甚至簡單的放到共用資料夾,開發人員即可透過 NuGet 取用。

以下就是對建立 NuGet 套件的基本步驟做介紹。

建立範例類別庫

範例專案透過 HtmlAgilityPack 套件提供一個極簡單的 Html Parsing 功能。

環境資訊:

  • Visual Studio 2017
  • .NET Framework 4.7.1

建立專案

  1. 建立一個類別庫專案,取名 HtmlParser。
  2. 透過 NuGet 參考 HtmlAgilityPack 套件。

加入類別

  1. 新增類別: NodeReader.cs
  2. 程式內容:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    using HtmlAgilityPack;

    namespace HtmlParser
    {
    public class NodeReader
    {
    public static string GetFirstNodeInnerHtml(string html, string xpath)
    {
    var htmlDoc = new HtmlDocument();
    htmlDoc.LoadHtml(html);

    return htmlDoc.DocumentNode.SelectSingleNode(xpath)?.InnerHtml;
    }
    }
    }

建置,成功。

NuGet CLI

套件的建立及發佈刪除都要透過 nuget.exe 這個命令列公用程式。

如果有安裝 Visual Studio,可能已設定好環境可直接使用,可打開命令視窗試執行 nuget 看看。

安裝 nuget.exe

  1. 到 官網下載 nuget.exe,選建議版本即可 (recommended latest)。
  2. 需要 4.1.0 以上版本才能將套件發行至 nuget.org。
  3. 可加到系統的 PATH 環境變數中以方便叫用。

新增 Nuget 套件設定檔

要在專案中加入 Nuget 套件設定檔才能進行打包。

  1. 在專案資料夾中(包含有 *.csproj 的目錄)開啟 “命令提示字元”。
  2. 執行 nuget spec 指令;正確執行後會回應 “已成功建立 ‘<專案名稱>.nuspec’。”。

完成後會建立 *.nuspec 檔案,它是一個 XML 格式的檔案,可用任何文字編輯器打開它。

設定套件資訊

初始的設定檔看起來像這樣:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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 2018</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>

必要屬性

  • id -
    套件識別碼,必須是唯一的。
    \$id\$ 會預設取專案名稱。
  • version -
    套件版本,遵循 major.minor.patch 模式。
    \$version\$ 預設先取 AssemblyInfo.cs 的 AssemblyInformationalVersion 值,如果沒有 AssemblyInformationalVersion 就取 AssemblyFileVersion 值,產生的 .nupkg 檔名也會用上這個版本資訊。
  • authors -
    以逗號分隔的套件作者清單。這些名稱會顯示在 nuget.org 的 NuGet 組件庫中,並用來交互參照相同作者的其他套件。
    \$author\$ 會預設取 AssemblyInfo.cs 的 AssemblyCompany 值。
  • description -
    顯示用的套件詳細描述。
    \$description\$ 會預設取 AssemblyInfo.cs 的 AssemblyDescription 值。

重要屬性

  • title -
    套件的易記標題,會用於畫面的顯示。
    \$title\$ 會預設取 AssemblyInfo.cs 的 AssemblyTitle 值。
  • owners -
    以逗號分隔的套件作者清單。這通常和 authors 是同一份清單,也可以設為組織名稱,將套件上傳至 nuget.org 時會忽略。  
    \$author\$ 會預設取 AssemblyInfo.cs 的 AssemblyCompany 值。

選擇性屬性

  • projectUrl -
    套件首頁的 URL。
  • licenseUrl -
    套件授權的 URL。
  • iconUrl -
    具有透明背景之 64x64 圖片的 URL。
  • requireLicenseAcceptance -
    指定在安裝套件時,用戶端是否必須提示取用者接受套件授權。
  • releaseNotes -
    此版本套件的變更描述。
  • tags -
    以逗號分隔的標記與關鍵字清單,可描述套件並提供搜尋和篩選的協助。

調整屬性值以符合專案內容。
保留不用的選擇性屬性預設內容時,在打包時會出現警告訊息。

進行打包

因為專案中有參考到 HtmlAgilityPack 套件,打包時會一併加入相依性參考。
(會利用 packages.config 來取得相依性。)

命令

基本打包
nuget pack {project_file}

手動指定套件版本號
nuget pack {project_file} -Version {version}

{project_file} 沒指定時會自動取用目錄下的 .csproj 或 .nuspec 檔,如範例的 HtmlParser.csproj。

輸出內容如下:

1
2
3
4
5
6
7
PS D:\Temp\dotnet\HtmlParser\HtmlParser> nuget pack
正在嘗試從 'HtmlParser.csproj' 建置封裝。
MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
正在封裝 'D:\Temp\dotnet\HtmlParser\HtmlParser\bin\Debug' 中的檔案。
正在使用中繼資料的 'HtmlParser.nuspec'。
找到 packages.config。使用列出的封裝做為相依性。
Successfully created package 'D:\Temp\dotnet\HtmlParser\HtmlParser\HtmlParser.1.0.0.4.nupkg'.

打包完成後資料夾底下就會出現 HtmlParser.1.0.0.4.nupkg 的套件檔。
.nupkg 檔其實是個 .zip 檔,可用解壓縮工具打開。

參考資料及圖片來源

  1. 官方-套件建立工作流程
  2. 官方- .nuspec 參考