1. 創建類庫項目
你可以使用現有的 .NET 類庫項目用於要打包的代碼,或者創建一個簡單的項目,.NET CORE 2.1 項目的 類庫如下所示:
NugetDemo.class
using System; namespace NugetTest { public class NugetDemo { public static String ReturnInfo() { return "生成自己的安裝包"; } } }
2:下載NuGet.exe
使用之前,需要先下載NuGet.exe, 設置機器的PATH環境變量,將其NuGet.exe的路徑添加到PATH變量中。我放在D:\system下,所以路徑為D:\system
https://www.nuget.org/downloads
選擇版本即可
4 打開 類庫項目文件 NugetTest.AssemblyInfo.cs
然后打開AssemblyInfo.cs
文件 NugetTest.AssemblyInfo.cs,將assembly
的屬性設置好,記得再設置一下AssemblyVersion
特性,以指定我們類庫的版本。
using System; using System.Reflection; using System.Runtime.InteropServices; [assembly: System.Reflection.AssemblyCompanyAttribute("Cloud")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [assembly: System.Reflection.AssemblyDescriptionAttribute("在線引用包")] [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")] [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")] [assembly: System.Reflection.AssemblyProductAttribute("Cloud")] [assembly: System.Reflection.AssemblyTitleAttribute("NugetTest")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] [assembly: System.Runtime.InteropServices.ComVisible(false)] [assembly: Guid("20182b9f-91de-4515-9c8c-ced3d61589e1")] // 由 MSBuild WriteCodeFragment 類生成。
其實也可以在 項目屬性中設置
通過CMD命令 執行
5 產生並修改nuspec
nuspec是NuGet將項目打包成nupkg的輸入文件,可以通過nuget spec
命令產生。在命令提示符下,進入NugetTest.csproj
文件所在目錄,然后執行:
類庫項目目錄就是自己項目存放的路徑地址
執行 nuget spec
生成的文件如下:
C:\Users\>cd C:\Users\Desktop\NugetTest\NugetTest
C:\Users\Desktop\NugetTest\NugetTest>nuget spec
已成功創建“NugetTest.nuspec”。
用記事本打開NugetTest.nuspec
文件,注意里面的$xxx$
宏,這些就是引用了AssemblyInfo.cs
中的設置值,在編譯產生package的時候,會使用AssemblyInfo.cs
中的相應值進行替換。完成編輯后,我們的nuspec文件如下:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>North</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>
<copyright>Copyright 2018</copyright>
</metadata>
</package>
不需要的節點去掉即可 以免下面執行 會有警告錯誤信息
6.產生類庫包(Library Package)
在NugetTest.csproj的路徑下,使用下面的命令產生類庫包:
nuget pack NugetTest.csproj
C:\Users\Desktop\NugetTest\NugetTest>nuget pack NugetTest.csproj
正在嘗試從“NugetTest.csproj”生成程序包。
MSBuild auto-detection: using msbuild version '15.8.169.51996' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin'.
正在打包“C:\Users\Desktop\NugetTest\NugetTest\bin\Debug\netcoreapp2.1”中的文件。
正在對元數據使用“NugetTest.nuspec”。
Successfully created package 'C:\Users\Desktop\NugetTest\NugetTest\NugetTest.1.0.0.nupkg'.
生成文件:
7.這樣就完整的生成一個類庫包 把這個包改成ZIP格式后就可以看到里面的DLL相關信息