在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>bin\Debug\</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>bin\Release\</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="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.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)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.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>bin\Debug\</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>bin\Release\</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%\NuGet\NuGet.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,這樣每次編譯自動生成到該目錄。

當然也可以使用腳本上傳到公共服務器
.nuget\NuGet.exe push .\TemporaryFile\bin\Debug\*.nupkg -apikey Admin:Admin -source http://localhost:81/nuget/Default
http://www.tuicool.com/articles/N7raEr





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM