在VS中自动生成NuGet包以及搭建自己的或单位内部的NuGet服务器

关于NuGet的介绍已经很多,可以参考下面的:
NuGet学习笔记(1)——初识NuGet及快速安装使用 http://kb.cnblogs.com/page/143190/
NuGet学习笔记(2)——使用图形化界面打包自己的类库 http://kb.cnblogs.com/page/143191/
NuGet学习笔记(3)——搭建属于自己的NuGet服务器  http://kb.cnblogs.com/page/143192/

上面的文章介绍了搭建Web版本的NuGet服务器以及用图形化的方式生成NuGet包。


上面的文章介绍了搭建Web版本的NuGet服务器,以及自动化生成NuGet包的方法


本文介绍简单的NuGet服务器,以及VS2013自动化生成NuGet包的内容。

一、VS自动生成NuGet包
1、在VS中创建类库项目




2、启用NuGet程序包还原(Enable NuGet Package Restore


        在解决方案上右击,选择“启用NuGet程序包还原”


       确定后会在解决方案中增加一个“.nuget"文件夹,该文件夹有三个文件。



     此时,打开项目文件ClassLibrary1.csproj,可以看到类似下面的内容,注意:下面绿色背景行的内容是否出现在ClassLibrary1.csproj中
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)$(MSBuildToolsVersion)Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{92A6F604-9829-49FB-8B06-FF2E4C757EC4}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary1</RootNamespace>
    <AssemblyName>ClassLibrary1</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..</SolutionDir>
    <RestorePackages>true</RestorePackages>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>binDebug</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>binRelease</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="PropertiesAssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
  <Import Project="$(SolutionDir).nugetNuGet.targets" Condition="Exists('$(SolutionDir).nugetNuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
  <PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  </PropertyGroup>
  <Error Condition="!Exists('$(SolutionDir).nugetNuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir).nugetNuGet.targets'))" />
  </Target>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project> 

如果没有出现带绿色背景行的内容,需要手动添加上。

第1块绿背景内的第1行代码创建了SolutionDir 并设置默认值为项目的父目录,NuGet.targets利用这个配置值来找到他需要的NuGet.exe等资源。第2行代码,RestorePackages被设置为True。

第2块绿背景内的代码创建了对.nuget文件夹下的NuGet.targets文件的引用,并添加了缺少NuGet.targets文件的错误信息。



3、设置BuildPackages为True


2中启用了NuGet还原,但是还需要设置让NuGet自动生成nupkg包文件,用记事本打开.csproj文件添加下面1行语句
<BuildPackage>true</BuildPackage>
到下图中1后面一行的话,Debug和Release编译均生成包
下图中2后面一行的话,只用Debug编译才生成包
下图中3后面一行的话,只用Release编译才生成包



例如:我只需要在Release编译时生成Nuget包,设置如下即可(倒数第二行加粗语句)。

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{92A6F604-9829-49FB-8B06-FF2E4C757EC4}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary1</RootNamespace>
    <AssemblyName>ClassLibrary1</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..</SolutionDir>
    <RestorePackages>true</RestorePackages>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>binDebug</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>binRelease</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <BuildPackage>true</BuildPackage>
  </PropertyGroup>
 
添加完之后,保存,退出,回到VS会自动提示重新载入,重新载入即可。

4、生成解决方案,默认创建两个包

执行”生成解决方案“就可以在bin文件夹下看到.nupkg文件了。默认生成两个.nupkg文件,一个是包含assemble的包,另一个是“Symbol”包(其中不仅有assemble同时还附带有debugging用到的源码等资源)

至此,就完成了自动生成NUGet包的工作了。有兴趣的可以接着看后面的内容。

5、”标准“NuGet包


前面的过程中,我们没有给NuGet提供配置信息,NuGet就会按照默认来配置这些包,并且使用project和assembly的信息来说明包该如何来配置。
用”NuGet Package Explorer“来打开NuGet包,就可以看到NuGet包的Id、Version、Title和Copyright 的值使用了 AssemblyInfo.cs文件中的定义。
如果AssemblyInfo.cs中的AssemblyCompany  没有设置,则使用计算机的用户名来设置NuGet包的Authors和Owners值。AssemblyDescription如果没有设置,使用“Description"来代替,并在编译时的输出中给出警告信息。如下所示:


6、定义包属性

只需要修改 AssemblyInfo.cs文件中的定义后,重新生成包时会自动从该文件读取相关内容。

其中关于版本号的描述如下:
在使用VS自带的版本生成系统时,如果让它自动生成版本的build number和revision number的话,默认情况下生成的build号是从2000年1月1日0点0分以来(到生成这个版本时)所经历的整天数,revision号是所经历的时间减去整天数之后,剩下的秒数除以2(不除以2的话,一天有8万多秒,超过了Int16的最大值).

但是,有时候只修改AssemblyInfo.cs文件中的值还不行,比如:把版本信息设置为
[assemblyAssemblyVersion("1.0.0")]
[assemblyAssemblyFileVersion("1.0.0")] 
的2-dot形式,但是生成之后仍然是3-dot的形式。这时候就需要使用“nuspec”元数据文件,这个文件不仅可以提供版本的设置,可以提供更多的设置。
如果nuspec文件和project名字相同,且和project的路径一致,那么NuGet就会优先使用nuspec文件中的设置,而不是优先使用AssemblyInfo.cs中的设置。

7、生成nuspec文件


可以使用NuGet.exe从Assemblies和project中生成nuspec文件,但是这种方式产生的nuspec文件通常包含很多不需要的从模板生成的设置,一种简单的方法是,用NuGet Package Explorer打开一个NuGet包文件,然后从File-“Save Metadata As…”保存得到一个nuspec文件,也可以用winrar,7-zip等打开nupkg从中复制一个。把这个文件复制到project目录后,可以添加到项目方便管理。

8、使用 nuget.targets来自动清除rebuil产生的旧版本


现在可以分享你的package了,如果开源,你可以分享到nuget.org。如果是私有代码,可以分享到自己的NuGet服务器。
但是,每次重新生成时,上次生成的不会清除,也就是说所有的老版本package仍然存在于生成文件夹,除非你手动删除。除了占用空间,这些packages还会拖慢分享的过程,如果使用NuGet Package Explorer分享包时,你需要不断滚动才能找到最新版本,使用命令行也很麻烦,这里介绍一个快速的使用 nuget.targets完成自动化删除旧版本方法。
nuget.targets已经在.nuget文件中了,打开添加三段内容(下面绿色背景的代码段),每一段内容的功能均已经注释。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!--3-6行定义了一个名称“OutputPackages”的ItemGroup,用来查找输出目录中的所有文件名以项目名称开头的NuGet packages文件--> 
  <ItemGroup>
    <OutputPackages Include="$(TargetDir)$(ProjectName)*.nupkg" />
  </ItemGroup>
  <PropertyGroup>
        <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)..</SolutionDir>
        <!-- Enable the restore command to run before builds -->
        <RestorePackages Condition="  '$(RestorePackages)' == '' ">false</RestorePackages>
        <!-- Property that enables building a package from a project -->
        <BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
        <!-- Determines if package restore consent is required to restore packages -->
        <RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
        <!-- Download NuGet.exe if it does not already exist -->
        <DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
    </PropertyGroup>
    <ItemGroup Condition=" '$(PackageSources)' == '' ">
        <!-- Package sources used to restore packages. By default, registered sources under %APPDATA%NuGetNuGet.Config will be used -->
        <!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
        <!--
            <PackageSource Include="https://www.nuget.org/api/v2/" />
            <PackageSource Include="https://my-nuget-source/nuget/" />
        -->
    </ItemGroup>
    <PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
        <!-- Windows specific commands -->
        <NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
        <!-- We need to launch nuget.exe with the mono command if we're not on windows -->
        <NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
    </PropertyGroup>
    <PropertyGroup>
        <PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
        <PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)packages.$(MSBuildProjectName).config</PackagesProjectConfig>
    </PropertyGroup>
    <PropertyGroup>
      <PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)packages.config')">$(MSBuildProjectDirectory)packages.config</PackagesConfig>
      <PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
    </PropertyGroup>
    
    <PropertyGroup>
        <!-- NuGet command -->
        <NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)NuGet.exe</NuGetExePath>
        <PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
        <NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
        <NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
        <PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\'))</PackageOutputDir>
        <RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
        <NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
        <PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
        <PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
        <!-- Commands -->
        <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
        <BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
        <!-- We need to ensure packages are restored prior to assembly resolve -->
        <BuildDependsOn Condition="$(RestorePackages) == 'true'">
            RestorePackages;
            $(BuildDependsOn);
        </BuildDependsOn>
        <!-- Make the build depend on restore packages -->
        <BuildDependsOn Condition="$(BuildPackage) == 'true'">
            $(BuildDependsOn);
            BuildPackage;
        </BuildDependsOn>
        <!--84-87行,指示MSBuid运行CleanPackages这个Target -->
        <CleanDependsOn Condition="$(BuildPackage) == 'true'">
            $(CleanDependsOn);
            CleanPackages;
        </CleanDependsOn>
    </PropertyGroup>
    <Target Name="CheckPrerequisites">
        <!-- Raise an error if we're unable to locate nuget.exe  -->
        <Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
        <!--
        Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
        This effectively acts as a lock that makes sure that the download operation will only happen once and all
        parallel builds will have to wait for it to complete.
        -->
        <MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
    </Target>
    <Target Name="_DownloadNuGet">
        <DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
    </Target>
    <Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">        
        <Exec Command="$(RestoreCommand)"
              Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
        <Exec Command="$(RestoreCommand)"
              LogStandardErrorAsError="true"
              Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
    </Target>
    <Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
        <Exec Command="$(BuildCommand)"
              Condition=" '$(OS)' != 'Windows_NT' " />
        <Exec Command="$(BuildCommand)"
              LogStandardErrorAsError="true"
              Condition=" '$(OS)' == 'Windows_NT' " />
    </Target>
    <!--123-125行,定义名字为CleanPackage的Target,使用MSBuild的内置Delete任务来删除3-6行定义的OutputPackages中的文件-->
    <Target Name="CleanPackages">
      <Delete Files="@(OutputPackages)"></Delete>
    </Target>
  <UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <OutputFilename ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
            <Reference Include="System.Core" />
            <Using Namespace="System" />
            <Using Namespace="System.IO" />
            <Using Namespace="System.Net" />
            <Using Namespace="Microsoft.Build.Framework" />
            <Using Namespace="Microsoft.Build.Utilities" />
            <Code Type="Fragment" Language="cs">
                <![CDATA[
                try {
                    OutputFilename = Path.GetFullPath(OutputFilename);
                    Log.LogMessage("Downloading latest version of NuGet.exe...");
                    WebClient webClient = new WebClient();
                    webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
                    return true;
                }
                catch (Exception ex) {
                    Log.LogErrorFromException(ex);
                    return false;
                }
            ]]>
            </Code>
        </Task>
    </UsingTask>
</Project>
首先在3-6行定义了一个名称“OutputPackages”的ItemGroup,用来查找输出目录中的所有NuGet packages。然后在123-125行,定义名字为CleanPackage的Target,使用MSBuild的内置Delete任务来删除3-6行定义的OutputPackages中的文件。最后在84-87行,指示MSBuid运行CleanPackages这个Target。

如果你不想自己添加,可以直接复制然后覆盖你的文件即可。

10、搭建自己的或单位内部的NuGet服务器


很多时候,我们公司或个人会有很多常用的dll,为了方便管理,其实我们可以搭建自己内部的NuGet管理方式。从下图可以看到

可以设置D:\MyPackages文件夹作为源,那么我们就可以把自己所有的dll统一放到D:\MyPackages文件夹中,设置可以设置所有的dll的Release生成路径为D:\MyPackages,这样每次编译自动生成到该目录。

当然也可以使用脚本上传到公共服务器
.nugetNuGet.exe push .TemporaryFileinDebug*.nupkg -apikey Admin:Admin -source http://localhost:81/nuget/Default
http://www.tuicool.com/articles/N7raEr




原文地址:https://www.cnblogs.com/liyanwei/p/af94dcd17cc45977968211162ecebabb.html