【问题标题】:Cake NuGetRestore always wants MSBuild14?Cake NuGetRestore 总是想要 MSBuild14?
【发布时间】:2017-03-31 06:46:51
【问题描述】:

在尝试在仅知道 Visual Studio 2017 的机器上使用 Cake v0.19.1 构建解决方案时,我似乎无法让 NuGetRestore 接受 MSBuildVersion = NuGetMSBuildVersion.MSBuild15 的设置。

是否有一些神奇的步骤可以将特定的 MSBuild 版本导入我缺少的 NuGetRestore

输出

...

========================================
RestoreNuGet
========================================
Executing task: RestoreNuGet
Failed to load msbuild Toolset
  Could not load file or assembly 'Microsoft.Build, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
An error occurred when executing task 'RestoreNuGet'.
Error: NuGet: Process returned an error (exit code 1).

精简的 build.cake

var target = Argument("target", "Default");
var solution = "./some-random.sln";

Task("Default")
.Does(() => {
    NuGetRestore(
        solution,
        new NuGetRestoreSettings {
            MSBuildVersion = NuGetMSBuildVersion.MSBuild15,
        }
    );
});

RunTarget(target);

更新:获取 NuGet v4

根据@devlead's answer,我将 build.ps1 文件指向 NuGet v4.0.0 并得到了这个输出。

Cannot find the specified version of msbuild: '15'
An error occurred when executing task 'RestoreNuGet'.
Error: NuGet: Process returned an error (exit code 1).

在我的完整 build.cake 中,我使用 vswhere 稍后使用 MSBuildSettings,我可以转储它找到的 MSBuild 路径(我确认 exe 存在于资源管理器中)。

C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/MSBuild/15.0/Bin/amd64/MSBuild.exe

【问题讨论】:

    标签: cakebuild


    【解决方案1】:

    您可以尝试将 MSBuild 别名与还原目标一起使用,最新版本的 MSBuild 应该具有内置 NuGet 支持。

    MSBuild(
        "./some.sln",
        configurator => configurator.WithTarget("restore"));
    

    【讨论】:

    • 看起来此配置器设置将起作用(结合$NUGET_URL 重定向答案以获得 NuGet v4)。您是否知道如何将其转换为 MSBuildSettings 构造函数变体?使用Targets 属性的明显解决方案失败了,因为它是只读的。
    • 我的构建工作可能是侥幸。我必须已经在 VS 中进行了还原。相反,这个特定步骤是说它没有做任何事情:Nothing to do. None of the projects specified contain packages to restore.
    【解决方案2】:

    确保您使用的是最新版本的 NuGet.exe,目前它是 v4.0.0 是最新版本,但您也可以在 https://dist.nuget.org 上查看可用列表

    如果您使用默认的build.ps1,您可以将其修改为始终下载特定版本的 NuGet.exe

    您可以通过删除 Test.Path 部分来执行此操作 - 这样它就不会在除您的工具文件夹之外的任何其他地方查找 nuget.exe。 然后通过 build.ps1 更改下载 uri 以不使用最新的稳定版(当前为 v3.5.0)而是使用特定版本

    $NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
    

    $NUGET_URL = "https://dist.nuget.org/win-x86-commandline/v4.0.0/nuget.exe"
    

    将确保您始终下载 exe 的v4.0.0

    也可以用一点 PowerShell 来验证工具中的正确版本,例如

    if ((Get-ChildItem $NUGET_EXE `
        | % VersionInfo `
        | % ProductVersion `
        | ? { $_ -eq '4.0.0' }|Measure-Object).Count -eq 1)
    {
       'Correct version'
    } else {
       'Incorrect version'
    }
    

    【讨论】:

    • 我试了一下,并用新的错误输出更新了问题。我想知道它是否在寻找 NuGetMSBuildVersion.MSBuild15 的错误位置。
    • 使用 NuGet v4,我只需从原来的 Restore 调用中删除 MSBuildVersion = NuGetMSBuildVersion.MSBuild15 即可成功恢复。
    猜你喜欢
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-05
    • 2013-06-28
    • 2020-06-11
    • 2019-03-03
    • 2018-02-14
    相关资源
    最近更新 更多