如何在 Nuget 釋出自己的類庫包
阿新 • • 發佈:2019-02-18
我們可以將自己實現的類庫釋出到 Nuget 上,方便在未來開發應用程式時,對已有程式碼或結構進行復用。以下是在 Nuget 釋出自己類庫的一個簡單介紹。 1. 首先,我們需要到 nuget.org 註冊並 獲取 API key 2. 隨後,下載 Nuget 命令列工具,建議將 nuget.exe 註冊到系統的環境變數中,方便後續步驟的操作 3. 在 CMD 中執行 Nuget 命令列工具,並將第一步中獲取的 API Key 進行註冊 [text] nuget setApiKey <my_api_key> [/text] 4. 在已經準備好的釋出專案(Project,而不是 Solution)的根目錄開啟 CMD,執行以下 Nuget 命令,從而建立 .nuspec 包輸入描述檔案 [text] nuget spec [/text] 執行結束後,將會生成一個與專案同名的 .nuspec 檔案 5. 使用文字編輯器,編輯生成的 .nuspec 檔案 <!>注意 關於此 XML 檔案的詳細描述,可以參考微軟的官方文件。下面以 Squirrel Framework 的 .nuspec 檔案作為一個示例。 [xml] <?xml version="1.0"?> <package> <metadata> <!-- 定義 Nuget Package 的包名,需要唯一,之後再發布新的版本也不需要更改 --> <id>SquirrelFramework</id> <!-- 定義 Package 的版本號,這裡採用引數形式,會自動從專案屬性的 Assembly Information 中讀取版本號,也可以直接指定版本字串,比如 1.0.0 --> <version>$version$</version> <!-- 定義 Package 的標題、描述、作者,以及所有者等 --> <title>Squirrel Framework</title> <description>A lightweight back-end framework and utilities kit based on MongoDB/Redis</description> <authors>Hendry</authors> <owners>Hendry</owners> <releaseNotes>The initial version of Squirrel Framework</releaseNotes> <!-- 指定 Package 的 License、專案地址、使用的圖示、是否會提示下載使用者確認 License 等 --> <licenseUrl>https://github.com/imnista/SquirrelFramework/blob/master/LICENSE</licenseUrl> <projectUrl>https://github.com/imnista/SquirrelFramework</projectUrl> <repository type="git" url="https://github.com/imnista/SquirrelFramework" /> <iconUrl>http://s.nap7.com/nugetsquirrelframework.png</iconUrl> <requireLicenseAcceptance>true</requireLicenseAcceptance> <copyright>Copyright 2018 Hendry, nap7.com</copyright> <!-- 定義 Package 的 tag 標籤,方便分類和搜尋 --> <tags>mongodb redis utility kit back end light repository dao squirrel framework ddd</tags> <!-- 定義當前 Nuget Package 對其他第三方 Package 的依賴 --> <dependencies> <dependency id="Newtonsoft.Json" version="11.0.1" /> <dependency id="MongoDB.Bson.signed" version="2.5.0" /> <dependency id="MongoDB.Driver.Core.signed" version="2.5.0" /> <dependency id="MongoDB.Driver.signed" version="2.5.0" /> <dependency id="StackExchange.Redis.StrongName" version="1.2.6" /> </dependencies> </metadata> <files> <!-- 定義編譯時行為 --> <file src="build\**" target="build" /> <!-- 當 Package 包含多個依賴專案時,在此新增內部的依賴專案 --> <file src="bin\Release\Common.Utility.dll" target="lib/net462" /> <file src="bin\Release\SquirrelFramework.Configurations.dll" target="lib/net462" /> <file src="bin\Release\SquirrelFramework.Domain.Model.dll" target="lib/net462" /> <file src="bin\Release\SquirrelFramework.Domain.Service.dll" target="lib/net462" /> <file src="bin\Release\SquirrelFramework.Repository.dll" target="lib/net462" /> </files> </package> [/xml] <!>注意 每個 nuget 包只對應一個 VS project,如果我們需要在 Nuget 中包含多個 DLL,則需要對這個 .nuspec 檔案進行配置,如上的 <files> 節點就是做這件事情的。我們的程式碼中需要包含多個 DLL 引用,將它們都列舉在此節點下,並給出其適應的 .NET 版本即可。 此外,如果我們的 Package 引用了其它第三方 Nuget Package,則不要在 <files> 節點裡指定,而是在 <metadata> 的 <dependencies> 節點進行設定,使用者安裝 Package 時,會自動安裝依賴專案。 6. 開始生成 Nuget Package (1) 在確定我們的 Project 版本號設定正確後(如果是再次釋出,記得將版本號遞增),首先在 Release 模式下 Rebuild 程式碼。(2) 隨後,在專案目錄開啟 CMD,執行以下 Nuget 命令。 ① 對於<strong> .NET Framework </strong>的 Project,執行以下命令即可: [text] nuget pack SquirrelFramework.csproj -Properties Configuration=Release -Properties NuspecFile=SquirrelFramework.nuspec [/text] ② 對於<strong> .NET Core </strong>的 Project,首先需要將要包含的 DLL, 明確列明在 .nuspec 的 <files> 節點中(如果不設定,將會出現 “assembly outside lib folder” 警告,並生成錯誤的 Package 包) [xml] <files> <file src="bin\Release\netstandard2.0\Common.Utility.dll" target="lib\netstandard2.0" /> </files> [/xml] 隨後執行: [text] dotnet pack --configuration release -p:NuspecFile=SquirrelFramework.Utility.nuspec [/text] 或 [text] msbuild /t:pack /p:Configuration=Release /p:NuspecFile=SquirrelFramework.nuspec [/text] <a href="http://nap7.com/me/wp-content/uploads/2018/04/NugetBuild.png"><img class="alignnone size-full wp-image-607" src="http://nap7.com/me/wp-content/uploads/2018/04/NugetBuild.png" alt="" width="1036" height="735" /></a> 關於 .NET Core 建立 package 的更多介紹,<a href="https://docs.microsoft.com/en-us/dotnet/core/deploying/creating-nuget-packages" rel="noopener" target="_blank">可參考這裡</a>。 <!>注意 如果目標是 .NET Core 的,不能用 nuget.exe 生成 Package,而是改用 msbuild,目前版本的 nuget.exe 並不支援 .NET Core!
另外,如果 .NET Core 工程使用了 .pfx 進行強簽名,dotnet 工具是不能正常生成 build 程式碼的。必須改用沒有密碼保護的 .snk 進行強簽名。
![](http://nap7.com/me/wp-content/uploads/2018/04/NugetBuild.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/NugetVSSettings.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/AddLocalNugetSource.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/SearchNugetPackageForTest.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/SquirrelFrameworkNuget.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/NugetCopyAlways.png)
![](http://nap7.com/me/wp-content/uploads/2018/04/BuildFolderForMSBuild.png)