【问题标题】:Build NuGet package for .Net Standard 2.0 lib with Cake使用 Cake 为 .Net Standard 2.0 lib 构建 NuGet 包
【发布时间】:2017-11-17 07:36:00
【问题描述】:

我创建了一个基于 .Net Standard 2.0 的类库,并使用 Cake 构建了项目/解决方案,但我找不到如何为该库构建 NuGet 包。 NuGetPack 不起作用。

【问题讨论】:

    标签: msbuild .net-standard cakebuild


    【解决方案1】:

    你真的有两个选择。 NuGetPack 将继续工作,我在我维护的几个 Cake Addins 上使用这种方法。您能否详细说明您所看到的问题?

    另一个选项是使用DotNetCorePack 别名。您可以在此文件中看到这一点(它是 Cake.Recipe 脚本集的一部分):

    https://github.com/cake-contrib/Cake.Recipe/blob/develop/Cake.Recipe/Content/nuget.cake

    它也记录在这里:

    https://cakebuild.net/api/Cake.Common.Tools.DotNetCore/DotNetCoreAliases/0F7A518E

    var projects = GetFiles(BuildParameters.SourceDirectoryPath + "/**/*.csproj")
        - GetFiles(BuildParameters.SourceDirectoryPath + "/**/*.Tests.csproj");
    
    var settings = new DotNetCorePackSettings {
        NoBuild = true,
        Configuration = BuildParameters.Configuration,
        OutputDirectory = BuildParameters.Paths.Directories.NuGetPackages,
        ArgumentCustomization = (args) => {
            if (BuildParameters.ShouldBuildNugetSourcePackage)
            {
                args.Append("--include-source");
            }
            return args
                .Append("/p:Version={0}", BuildParameters.Version.SemVersion)
                .Append("/p:AssemblyVersion={0}", BuildParameters.Version.Version)
                .Append("/p:FileVersion={0}", BuildParameters.Version.Version)
                .Append("/p:AssemblyInformationalVersion={0}", BuildParameters.Version.InformationalVersion);
        }
    };
    
    foreach (var project in projects)
    {
        DotNetCorePack(project.ToString(), settings);
    }
    

    【讨论】:

      【解决方案2】:

      NuGetPack 需要 nuget.exe 4.4.x 或更高版本才能使 .NET Standard 正常工作,也就是说,还有使用 nuget.exe 的替代方法。

      如果您使用的是 .NET Core Cli,则使用 DotNetCorePack 别名。

      如果您使用 MSBuild 进行构建,则新 SDK csproj 的 MSBuild now 有一个 Pack 目标,可能如下所示:

      var configuration           = Argument("configuration", "Release");
      FilePath solution           = MakeAbsolute(File("./src/MySolution.sln"));
      DirectoryPath artifacts     = MakeAbsolute(Directory("./artifacts"));
      var version = //some version login i.e. GitVersion
      var semVersion = //some version login i.e. GitVersion
      
      Func<MSBuildSettings,MSBuildSettings>
          commonSettings         = settings => settings
                                      .UseToolVersion(MSBuildToolVersion.VS2017)
                                      .SetConfiguration(configuration)
                                      .SetVerbosity(Verbosity.Minimal)
                                      .WithProperty("PackageOutputPath", artifacts.FullPath)
                                      .WithProperty("VisualStudioVersion", "15.0")
                                      .WithProperty("Version", semVersion)
                                      .WithProperty("AssemblyVersion", version)
                                      .WithProperty("FileVersion", version);
      
      Task("Clean")
        .Does(() =>
        {
          //some clean logic
        });
      
      Task("Restore-NuGet-Packages")
          .IsDependentOn("Clean")
          .Does(() =>
      {
          NuGetRestore(solution,
              new NuGetRestoreSettings {
                  Verbosity = NuGetVerbosity.Quiet
                  });
      });
      
      Task("Build")
          .IsDependentOn("Restore-NuGet-Packages")
          .Does(() =>
      {
          MSBuild(solution, settings => commonSettings(settings).WithTarget("Build"));
      });
      
      
      
      Task("Create-NuGet-Package")
          .IsDependentOn("Build")
          .Does(() =>
      {
           MSBuild(project,
              settings => commonSettings(settings)
                              .WithTarget("Pack")
                              .WithProperty("IncludeSymbols","true"));
      });
      

      【讨论】:

        【解决方案3】:

        我无法让NuGetPack 工作,尽管那可能是 NuGet exe 版本(尽管我相当确定它是 4.4.something)。

        我最终在我的蛋糕脚本中使用了以下代码来打包核心项目:

        private void PackCoreProject(string nugetVersion, string outputDirectory, string projectFile)
        {
            var settings = GetCorePackSettings(nugetVersion, outputDirectory);
            DotNetCorePack(projectFile, settings);
        }
        
        private DotNetCorePackSettings GetCorePackSettings(string nugetVersion, string outputDirectory)
        {
            // Version fun with dotnet core... https://stackoverflow.com/questions/42797993/package-version-is-always-1-0-0-with-dotnet-pack
        
            Func<Cake.Core.IO.ProcessArgumentBuilder, Cake.Core.IO.ProcessArgumentBuilder> appendVersionToArgs = 
                new Func<Cake.Core.IO.ProcessArgumentBuilder, Cake.Core.IO.ProcessArgumentBuilder>(_ => 
            {
                return _.Append($"/p:Version={nugetVersion}");
            });
        
            return new DotNetCorePackSettings
            {
                VersionSuffix = GetNuGetVersionSuffix(),
                OutputDirectory = outputDirectory,
                Configuration = configuration,
                NoBuild = true,
                IncludeSymbols = true,
                IncludeSource = true,
                ArgumentCustomization = appendVersionToArgs,
            };
        }
        
        private string GetNuGetVersion()
        {
            // If the branch is master or hotfix, build a normal version as these are due for release.
            if (IsReleaseBranch())
            {
                return $"{major}.{minor}.{build}";
            }
        
            return $"{major}.{minor}.0-{GetNuGetVersionSuffix()}";
        }
        

        请注意,我必须通过构建器功能手动将版本添加到末尾。

        IsReleaseBranch 方法只是一个开关,将 masterhotfix 标记为有效的发布分支,并将其他任何内容标记为 dev,因此当确定 NuGet 包版本时,dev build 是预发布的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多