隨着公司業務慢慢的拓展,項目便會越來越來多,很多項目會依賴其他項目DLL,比如一些底層的技術框架DLL引用,還有各業務系統的也有可能會有引用的可能。
項目多,交叉引用多,如果要是有一個DLL更新,那就要更新所有引用該DLL的項目,手動更新的話,有時候找到都很困難,更別說更新了,長此以往,更新DLL會變得很困難,那有什么辦法來解決這個問題 ?
對NuGet,話說微軟真是為廣大用戶着想,整出這么個好東西。我估計微軟內部DLL也亂得不像樣子,然后才有NuGet 這個產物。NuGet 管理程序包工具,Visual Studio 2012&2013 完全集成了這套工具。
但是今天講的是如何在公司內部搭建NuGet Server,來管理公司內部程序包。使內部程序包引用容易,更新版本容易。廢話就不多說了,直接入正題。
搭建NuGet Server 是不是建一個Web站點一樣,然后掛在IIS上面? 對的,就是這么回事,建一個網站,管理程序包,能夠上傳,能夠下載,且能夠做簡單版本管理。
我要說的是,站點確實要建,但是怎么管理程序包,上傳,下載也好等,大家不要擔心這些都有人已經做好了,只要拿來使用就可以了。接下來搞真的了。
一. NuGetServer 搭建和配置
1. 創建一個 “NuGetServerSolution” 解決方案,然后新增 “NuGetServer” Asp.Net 網站 或者 應用程序 空 項目。結構如下圖
2. 在 “NuGetServer” 項目上,右鍵選擇 “管理NuGet程序包” ,選擇 “聯機” ,右上角搜索框中輸入“NuGet.Server” Enter,在搜索結果中選擇 NuGet.Server 項,進行安裝,如圖
如果安裝最后,提示 替換 Web.config ,請選擇 全是。
3. 編譯“NuGetServer”項目,如果沒有出異常,這里就創建項目完成,NuGetServer 就這么簡單建成,稍后我部署到IIS上面,看看是不是真的可以了,先來講一下這個網站WebConfig 要配置地方。
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 -->
<configuration>
<appSettings>
<!-- Determines if an Api Key is required to push\delete packages from the server. -->
<add key="requireApiKey" value="true" />
<!-- Set the value here to allow people to push/delete packages from the server. NOTE: This is a shared key (password) for all users. -->
<add key="apiKey" value="0226651E-19EE-4F93-A172-5250C84DB16C" />
<!-- Change the path to the packages folder. Default is ~/Packages. This can be a virtual or physical path. -->
<add key="packagesPath" value="" />
<!-- Set allowOverrideExistingPackageOnPush to false to mimic NuGet.org's behaviour (do not allow overwriting packages with same id + version). -->
<add key="allowOverrideExistingPackageOnPush" value="false" />
<!-- Set ignoreSymbolsPackages to true to filter out symbols packages. Since NuGet.Server does not come with a symbol server, it makes sense to ignore this type of packages. When enabled, files named `.symbols.nupkg` or packages containing a `/src` folder will be ignored. If you only push .symbols.nupkg packages, set this to false so that packages can be uploaded. -->
<add key="ignoreSymbolsPackages" value="true" />
<!-- Set enableDelisting to true to enable delist instead of delete as a result of a "nuget delete" command. - delete: package is deleted from the repository's local filesystem. - delist: - "nuget delete": the "hidden" file attribute of the corresponding nupkg on the repository local filesystem is turned on instead of deleting the file. - "nuget list" skips delisted packages, i.e. those that have the hidden attribute set on their nupkg. - "nuget install packageid -version version" command will succeed for both listed and delisted packages. e.g. delisted packages can still be downloaded by clients that explicitly specify their version. -->
<add key="enableDelisting" value="false" />
<!-- Set enableFrameworkFiltering to true to enable filtering packages by their supported frameworks during search. -->
<add key="enableFrameworkFiltering" value="false" />
<!-- When running NuGet.Server in a NAT network, ASP.NET may embed the erver's internal IP address in the V2 feed. Uncomment the following configuration entry to enable NAT support. -->
<!-- <add key="aspnet:UseHostHeaderForRequestUrl" value="true" /> -->
</appSettings>
<system.web>
<httpRuntime maxRequestLength="31457280" />
<compilation debug="true" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".nupkg" mimeType="application/zip" />
</staticContent>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
</configuration>
第一點: 程序包發布,存放的路徑,在WebConfig appSettings PackagesPath 這個Key 設置,默認存放在部署的網站根目錄Packages文件夾。
第二點:appSettings requireApiKey 這個key 如果設置成 true , appSettings apiKey 是必須要設置的,這個就像密碼一樣。可以阻止不知道這個apiKey人訪問到程序包
4. 部署到IIS, 打開IIS ,右鍵選擇 “網站” ,添加網站,填寫 網站名稱(NuGetServer),選擇物理路徑,端口號 改成 "1000" 確定, 如下圖
5. 運行 NuGetServer 網站 "http://localhost:1000/", 出現下圖效果,則說NuGet 服務器 已經建立完成。
6. 接着配置Visual Studio 連接 NuGetServer。選擇“工具”菜單,選擇“選項”,彈出“選項”界面,選擇 “NuGet Package Manager” ,然后在選擇 “程序包源”,
點擊 “+”,在界面下方 設置 名稱 “mynuget.org” 隨便取,設置 源 “http://localhost:1000/nuget” (是不是上圖有說),確定 關閉界面,回到項目。如下圖
在項目右鍵,選擇“管理NuGet程序包”,聯機,下面是不是多出了一個 “mynuget.org” 程序源呢,雖然下面還沒有發布公司內部的程序包。如下圖
到此,NuGetServer 服務器,就搭建並配置完成。接一下來就是如何發布程序包,以及安裝程序包了。
二. 發布程序包,以及安裝程序包
1. NuGet Package Explorer
將程序包發布到NuGetServer,還要介紹到另外一個工具“NuGet Package Explorer”,這個工具是NuGetServer 程序包一個可視化的工具,它功能很多,可瀏覽已經發布的程序包信息,可以發布新的程序包(設置程序包版本,已經依賴程序包等),可以刪除發布的程序包。
CodePlex:https://npe.codeplex.com/
GitHub:https://github.com/NuGetPackageExplorer
2.在CodePlex 網站上,下載 NuGet Package Explorer , 安裝完成后,桌面會多出一個 “NuGet Package Explorer” 圖標,如下圖
3. 為了方便Demo,再創建一個 解決方案 “NuGetServerDemoSolution”,添加“NuGetServerDemo” 控制台項目,再添加 “NuGetServerDemoDLL” 類庫項目,結構如下圖。
“NuGetServerDemoDLL” 項目 主要會做成程序包發布
“NuGetServerDemo” 項目 安裝“NuGetServerDemoDLL” 程序包
4 . 打開 桌面 “NuGet Package Explorer” ,界面如下
圖片選項, 分別 意思是,1. 打開本地的nupkg,nuspec 文件。2. 打開指定 NuGetServer 所有的程序包列表。3.創建一個新程序包。4. 文檔
5.把 “NuGetServerDemoDLL” 發布到NuGetServer,點 “Create a new package” 未設置前截圖如下
上圖分為兩個編輯區,一個是 Package Metadata 負責描述程序包信息的,Package Contents 負責程序包文件相關的。
點擊 Package Metadata 區 “編輯” 按鈕,想編輯 “NuGetServerDemoDLL” 程序包描述信息。
然后 將“NuGetServerDemoDLL” 項目 產生Dll,拖入 Package Contents 最后效果如下圖
點擊 上圖 綠色的 √ 關閉編輯Package Metadata , 點擊 ”File“ 菜單,選擇 Publish 發布程序集,填寫 PublishUrl(NuGetServer),PublishKey(apiKey),填寫完成 點擊“Publish” 發布,如果下方提示 “Package published successfully”,則發布成功。如下圖。
6. 回到“NuGetServerDemoSolution” 解決方案,右鍵“NuGetServerDemo”,選擇“管理NuGet程序包”,選擇聯機下“mynuget.org”,安裝“NuGetServerDemoDLL” 程序包,如下圖
主要看圖左邊的 ,是不是“NuGet Package Explorer” 中設置過的一些程序包信息。
7. 用“NuGet Package Explorer”查看 NuGetServer 以發布程序包,選擇“File”菜單,選擇"Open from feed", 就會查詢到指定 NuGetServer 發布程序包,如下圖。
版本變更了,更新DLL 這邊就不來說了,大家自己摸索一下。謝謝。
好了,整個博文結束,這里想再提一下 NuGetServer 服務器部分,我這里也只是拋磚引玉一下,還“NuGet Package Explorer”也是,大家有空可以用點時間深入研究一下。
NuGetServer 源代碼 由於今天還在下載GitHub工具(網速非常之慢),明天會抽空,在提供GitHub地址。
NuGetServer 源代碼 : https://github.com/haibozhou1011/NuGetServer
另為,今天在博客園里面右邊則欄加一個“打賞”功能,就支持微信,支付寶。大家玩一下。謝謝!