在公司内部有很多基礎架構或者基礎元件,甚至對于使用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.
作者:
lovecindywang本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。