2

I have created a class library based on .Net Standard 2.0 and I build the project/solution with Cake, but I cannot find how to build NuGet package for that library. NuGetPack doesn't work.

fxdxpz
  • 1,969
  • 17
  • 29

3 Answers3

2

You really have two options. NuGetPack will continue to work, I use this approach on a couple of Cake Addins that I maintain. Could you explain more details about the issue that you are seeing?

The other option would be to use the DotNetCorePack alias. You can see this in action in this file (which is part of the Cake.Recipe set of scripts):

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

And it is also documented here:

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);
}
Gary Ewan Park
  • 17,610
  • 5
  • 42
  • 60
1

NuGetPack requires nuget.exe 4.4.x or newer for .NET Standard to work properly, that said there's also alternatives to using nuget.exe.

If you're using the .NET Core Cli then you use the DotNetCorePack alias.

If you're using MSBuild to build, MSBuild now for new SDK csproj has a Pack target, which could look something like this:

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"));
});
devlead
  • 4,935
  • 15
  • 36
0

I couldn't get NuGetPack to work, though that may have been the NuGet exe version (though I was fairly certain it was 4.4.something).

I ended up with the following code in my cake script for packaging Core projects:

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()}";
}

Note I have to manually add the version to the end via the builder function.

The IsReleaseBranch method is just a switch that flags master or hotfix as a valid release branch and anything else as dev, so when a NuGet package version is determined, dev builds are pre-release.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187