在內部架設NuGet服務器
在公司內部有很多基礎框架或者基礎組件,甚至對于使用SOA架構的公司來說,會有大量的業務組件的契約程序集,對于這些框架或組件的引用管理有的人使用源代碼管理工具,但是NuGet相比源代碼管理工具更方便:
1) 安裝和卸載:不需要手動添加和移除引用,不需要手動改寫配置文件甚至是一些初始化服務的代碼。版本升級也只需要執行一條命令。
2) 打包:多文件打包,支持依賴管理等,使用的人沒有繁瑣的配置。
對于官方的包,可以在http://www.nuget.org/ 找到,自己也可以提交包上去。但是如果不希望把包公開的話,可以在內部架設一個NuGet服務器。
下面介紹一下基本步驟以及如何進行打包。
1) 下載 NuGetServer.rar (包含源代碼,改編自mceranski-nugetserver,找不到原始下載地址了)編譯后,發布到內網服務器上。這個MVC3網站有幾個功能:
一是提供Nuget的服務,提供所有包的信息,供VS2010中NuGet包管理器使用
二是提供了幾個頁面,可以上傳包也可以瀏覽所有的包
三是提供了一個web服務,可以供程序在編譯后自動上傳包
2) 下載 Lib.rar (僅是可執行文件),解壓縮到解決方案目錄下的Lib目錄中。這個壓縮包里提供了兩個程序:
一是官網提供的NuGet.exe小工具,可以打包文件稱nupkg
二是自己寫的一個上傳包到NuGet服務端Web服務的小工具,這里是源代碼,它會上傳最新編譯的那個包
3) 配置需要打包的項目的屬性:
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,設置為:
文件內容和普通的配置文件無異:
<?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服務端。
如果沒有上傳成功,請檢查NuGetPackageUploader.exe.config中的地址是否修改為你部署的服務端的地址。
6) 在官網安裝了VS2010的NuGet包管理器插件之后:
配置一下NuGet服務端地址應該就可以看到自己上傳的所有包了:
如果你的網站部署到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.





浙公網安備 33010602011771號