在内部架设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

相关文章
相关标签/搜索