搭建公司內部的NuGet Server

     隨着公司業務慢慢的拓展,項目便會愈來愈來多,不少項目會依賴其餘項目DLL,好比一些底層的技術框架DLL引用,還有各業務系統的也有可能會有引用的可能。git

項目多,交叉引用多,若是要是有一個DLL更新,那就要更新全部引用該DLL的項目,手動更新的話,有時候找到都很困難,更別說更新了,久而久之,更新DLL會變得很困難,那有什麼辦法來解決這個問題 ? github

     對NuGet,話說微軟真是爲廣大用戶着想,整出這麼個好東西。我估計微軟內部DLL也亂得不像樣子,而後纔有NuGet 這個產物。NuGet 管理程序包工具,Visual Studio 2012&2013 徹底集成了這套工具。web

     可是今天講的是如何在公司內部搭建NuGet Server,來管理公司內部程序包。使內部程序包引用容易,更新版本容易。廢話就很少說了,直接入正題。api

     搭建NuGet Server 是否是建一個Web站點同樣,而後掛在IIS上面? 對的,就是這麼回事,建一個網站,管理程序包,可以上傳,可以下載,且可以作簡單版本管理。服務器

 我要說的是,站點確實要建,可是怎麼管理程序包,上傳,下載也好等,你們不要擔憂這些都有人已經作好了,只要拿來使用就能夠了。接下來搞真的了。微信

一. NuGetServer 搭建和配置app

1. 建立一個 「NuGetServerSolution」 解決方案,而後新增 「NuGetServer」 Asp.Net 網站 或者 應用程序 空 項目。結構以下圖框架

2. 在 「NuGetServer」 項目上,右鍵選擇 「管理NuGet程序包」 ,選擇 「聯機」 ,右上角搜索框中輸入「NuGet.Server」  Enter,在搜索結果中選擇 NuGet.Server 項,進行安裝,如圖ide

若是安裝最後,提示 替換 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

     另爲,今天在博客園裏面右邊則欄加一個「打賞」功能,就支持微信,支付寶。你們玩一下。謝謝!

相關文章
相關標籤/搜索