如何制作一個專業的NuGet包?手把手教!超級詳細!絕對能學會!
目錄
一,在NuGet上創建并獲取API Keys
1.首先需用登錄,直接用微軟賬戶登錄即可

2.點擊右上角菜單API Keys,創建Key

3.填寫信息
我這里直接填寫項目名作為Key Name

4.獲取Key
點擊Copy按鈕獲取Key

二,創建項目
1.創建項目
框架使用.NET Framework 4,這個在后面也會使用到,輸出類型使用類庫

2.生成dll
完成,項目后,使用Release模式生成dll

3.建立發布文件夾
為了更好管理文件,在E:\nuget新建WinForm.MoveControl文件夾,存放所需的文件

說明:
Icon.png文件,作為nuget包的圖標,發現jpg不支持,只能用png
readme.md文件,介紹文件,會在https://www.nuget.org/packages/WinForm.MoveControl/顯示
WinForm.MoveControl.dll文件,項目文件
三,上傳NuGet
上傳NuGet的方法有兩種
1.使用命令行上傳,需要下載nuget.exe
2.使用界面工具上傳,需要下載NuGet Package Explorer
1.使用命令行上傳
1.1到https://www.nuget.org/downloads下載nuget.exe

1.2配置nuget環境變量
把下載的nuget.exe放到E:\nuget
打開電腦屬性--高級系統設置--環境變量--系統變量,選擇Path--編輯--新建--填寫E:\nuget,確定

環境變量配置好,就可以使用nuget指令了
1.3生成nuspec文件
使用nuget spec命令生產.nuspec 文件,文件xml格式的
-
-
<package >
-
<metadata>
-
<id>Package</id>
-
<version>1.0.0</version>
-
<authors>GreAmbWang</authors>
-
<owners>GreAmbWang</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>Package description</description>
-
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
-
<copyright>Copyright 2021</copyright>
-
<tags>Tag1 Tag2</tags>
-
<dependencies>
-
<dependency id="SampleDependency" version="1.0" />
-
</dependencies>
-
</metadata>
-
</package>
可以對里面的信息進行修改,我不悉習慣這種方式,對里面的參數,在下面會介紹到
1.4生成nupkg文件
使用nuget pack命令生產.nupkg文件

1.5上傳到nuget
直接使用功能命令
nuget push Package.1.0.0.nupkg xxxkey -Source https://api.nuget.org/v3/index.json
這樣就上傳完成了,還是第二種方法,更加推薦第二種
2.使用NuGet Package Explorer發布
2.1下載NuGet Package Explorer
https://www.microsoft.com/zh-cn/p/nuget-package-explorer/9wzdncrdmdm3?activetab=pivot:overviewtab
NuGet Package Explorer可以直接打開一個nupkg格式的文件
2.2參考log4net編寫格式
我們直接打開一個發布的nuget,就看看log4net

用NuGet Package Explorer打開,可以參考里面的編寫

2.3創建新Package

2.4新增lib文件夾

2.5新增net40文件夾

2.6添加已有文件

添加好文件,是這樣的

2.7編輯上傳數據
我這里選擇Edit Metadata,Edit Metadata Source則是xml文件格式的

填寫信息

還需添加項目依賴,則依賴哪些框架比如.NETFramework,.NETStandard等

編輯完成,看下效果

而Edit Metadata Source里面的xml格式是這樣的
-
-
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
-
<metadata>
-
<id>WinForm.MoveControl</id>
-
<version>1.0.5</version>
-
<title>WinForm.MoveControl 1.0.5</title>
-
<authors>GreAmbWang</authors>
-
<owners>GreAmbWang</owners>
-
<requireLicenseAcceptance>false</requireLicenseAcceptance>
-
<icon>Icon.png</icon>
-
<projectUrl>https://greambwang.blog.csdn.net/article/details/118424770</projectUrl>
-
<description>WinForm.MoveControl 能設置控件拖拽,調整控件大小和位置。
-
使用非常簡單,執行一句代碼,例如button1.SetMove();</description>
-
<summary>設置WinForm控件拖拽,調整控件大小和位置</summary>
-
<releaseNotes>發布1.0.5</releaseNotes>
-
<copyright>Copyright ? 2021 GreAmbWang</copyright>
-
<language>zh-Hans-CN</language>
-
<tags>GreAmbWang, WinForm</tags>
-
<readme>readme.md</readme>
-
<dependencies>
-
<group targetFramework=".NETFramework4.0" />
-
</dependencies>
-
</metadata>
-
</package>
保存nupkg文件

文件

2.8發布到nuget

輸入在nuget.org上的key

發布后,需要審核,大概兩分鐘就可以了

在NuGet上安裝

哈哈,完成
四,NuGet包的管理
1.管理包

NuGet Package Explorer,nuget.org與NuGet中,信息的對比

2.刪除
nuget包無法刪除,但是隱藏對應的版本

在Listing中,把不需要的版本去除勾選,則在NuGet搜索中不顯示出來

發布錯的包,都可以通過隱藏版本來設置

這是我上次的一個項目
WinForm.MoveControl
能設置控件拖拽,調整控件大小和位置。
使用非常簡單,執行一句代碼,例如button1.SetMove();
項目文章介紹:
https://greambwang.blog.csdn.net/article/details/118424770

GitHub:https://github.com/GreAmbWang/WinFormProjects
nuget:https://www.nuget.org/packages/WinForm.MoveControl/
參考
https://blog.csdn.net/qq_35260798/article/details/108467688
https://docs.microsoft.com/zh-cn/nuget/nuget-org/overview-nuget-org

浙公網安備 33010602011771號