使用Cake为.Net Standard 2.0 lib构建NuGet包

时间:2017-11-17 07:36:00

标签: msbuild .net-standard cakebuild

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

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:1)

NuGetPack需要nuget.exe 4.4.x或更新版本的.NET Standard才能正常工作,这表示还有使用nuget.exe的替代方法。

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

如果你正在使用MSBuild进行构建,现在MSBuild用于新的SDK csproj有一个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"));
});

答案 2 :(得分:0)

我无法让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构建是预发布。