在內部架設NuGet服務器

在公司內部有不少基礎框架或者基礎組件,甚至對於使用SOA架構的公司來講,會有大量的業務組件的契約程序集,對於這些框架或組件的引用管理有的人使用源代碼管理工具,可是NuGet相比源代碼管理工具更方便:html

1) 安裝和卸載:不須要手動添加和移除引用,不須要手動改寫配置文件甚至是一些初始化服務的代碼。版本升級也只須要執行一條命令。web

2) 打包:多文件打包,支持依賴管理等,使用的人沒有繁瑣的配置。redis

對於官方的包,能夠在http://www.nuget.org/ 找到,本身也能夠提交包上去。可是若是不但願把包公開的話,能夠在內部架設一個NuGet服務器。服務器

下面介紹一下基本步驟以及如何進行打包。架構

1) 下載 NuGetServer.rar (包含源代碼,改編自mceranski-nugetserver,找不到原始下載地址了)編譯後,發佈到內網服務器上。這個MVC3網站有幾個功能:app

一是提供Nuget的服務,提供全部包的信息,供VS2010中NuGet包管理器使用框架

二是提供了幾個頁面,能夠上傳包也能夠瀏覽全部的包less

三是提供了一個web服務,能夠供程序在編譯後自動上傳包工具

2) 下載 Lib.rar (僅是可執行文件),解壓縮到解決方案目錄下的Lib目錄中。這個壓縮包裏提供了兩個程序:網站

一是官網提供的NuGet.exe小工具,能夠打包文件稱nupkg

二是本身寫的一個上傳包到NuGet服務端Web服務的小工具,這裏是源代碼,它會上傳最新編譯的那個包

3) 配置須要打包的項目的屬性:

image

IF NOT "$(ConfigurationName)"=="Release" EXIT /B 0 
IF NOT EXIST $(SolutionDir)ReleasePackages MD $(SolutionDir)ReleasePackages 
$(SolutionDir)Libs\NuGet.exe Pack $(ProjectDir)$(ProjectName).nuspec -o $(SolutionDir)ReleasePackages\ 
$(SolutionDir)Libs\NuGetPackageUploader.exe $(SolutionDir)ReleasePackages\

這段腳本完成的功能是:

若是是Release方式編譯的話,先建立ReleasePackages文件夾,而後調用NuGet.exe 打包,最後調用NuGetPackageUploader.exe 上傳包

4) 在項目中建立[項目名].nuspec,包描述文件:

<?xml version="1.0" encoding="utf-8"?> 
<package> 
  <metadata> 
    <id>WcfExtension</id> 
    <version>1.0.0.0</version> 
    <title>WcfExtension</title> 
    <authors>做者</authors> 
    <projectUrl>項目地址</projectUrl> 
    <description>A communication framework based on Wcf</description> 
  </metadata> 
  <files> 
    <file src="bin\Release\*.dll" target="lib" /> 
    <file src="bin\Release\*.transform" target="content" /> 
  </files> 
</package> 
在這裏,咱們把全部的dll打入包,而且還把用於轉換配置文件的transform文件打入包。

爲了自動在配置文件中增長節點,咱們在項目文件下建立app.config.transform和web.config.transform,設置爲:

image

文件內容和普通的配置文件無異:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
  <configSections> 
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" /> 
  </configSections> 
  <appSettings> 
    <add key="configservice_address" value="192.168.129.11:1888/WcfConfigService.svc" /> 
    <add key="logservice_address" value="192.168.129.12:1889/WcfLogService.svc" /> 
    <add key="redis_address" value="192.168.129.175" /> 
    <add key="redis_message_client_channel" value="WcfConfigClientChange"/> 
    <add key="redis_message_service_channel" value="WcfConfigServiceChange"/> 
  </appSettings> 
  <unity configSource="unity.config" /> 
</configuration>

5) 以release編譯項目以後,能夠發現ReleasePackages中多了一個包,而且這個包會上傳到遠程的NuGet服務端。

image

若是沒有上傳成功,請檢查NuGetPackageUploader.exe.config中的地址是否修改成你部署的服務端的地址。

6) 在官網安裝了VS2010的NuGet包管理器插件以後:

image

配置一下NuGet服務端地址應該就能夠看到本身上傳的全部包了:

image

若是你的網站部署到nuget.xxx.com,那麼這裏的地址填寫nuget.xxx.com/nuget就能夠了。

找到包點擊Install按鈕就能夠安裝上這個組件了。

打開包管理器控制檯,輸入get-help NuGet,能夠看到其它的一些命令:

------------------        ---------------------------------------------- 
Get-Package                Gets the set of packages available from the package source.

Install-Package            Installs a package and its dependencies into the project.

Uninstall-Package        Uninstalls a package. If other packages depend on this package, 
                        the command will fail unless the –Force option is specified.

Update-Package            Updates a package and its dependencies to a newer version.

New-Package                Creates a new package when supplied with a Nuspec package specification file.

Add-BindingRedirect        Examines all assemblies within the output path for a project and adds binding 
                        redirects to the application (or web) configuration file where necessary. 
                    
Get-Project                Returns a reference to the DTE (Development Tools Environment) for the active 
                        or specified project.

 

轉自:http://www.cnblogs.com/lovecindywang/archive/2011/05/12/2044301.html

相關文章
相關標籤/搜索