6

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

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

输出

...

========================================
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 的回答,我将 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
4

2 回答 2

4

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

MSBuild(
    "./some.sln",
    configurator => configurator.WithTarget("restore"));
于 2017-03-30T22:46:51.917 回答
2

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

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

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

$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'
}
于 2017-03-30T22:06:37.200 回答