visual-c++ - 如何让 MSBuild 自动下载解决方案/项目中引用的新 nuget 包?

标签 visual-c++ msbuild nuget googletest nuget-package-restore

我正在从为我的项目从源构建 googletest 切换到在 Visual Studio 2019 中使用 nuget 包 Microsoft.googletest.v140.windesktop.msvcSTL.static.rt-dyn.1.8.1。预警,这个这是我第一次涉足 Nuget,所以这个问题可能是我遗漏的非常基本的问题。

在 VS IDE 中构建/运行时,一切正常(据我所知)。但是,在我的构建机器上(使用 MSbuild 自动构建并从源代码控制提交触发,测试项目无法构建并出现错误:

注意:显然下面的路径已更改,但我确保目录嵌套级别相同

C:\Path\To\Project\SolutionName\ProjectName\TestsProject\TestProject.vcxproj(215,5): error : This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ....\packages\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.1.8.1\build\native\Microsoft.googletest.v140.windesktop.msvcstl.static.rt-dyn.targets.

自动构建过程使用以下 MSBuild 命令构建:

msbuild ""C:\Path\To\Project\SolutionName\SolutionFile.sln"" /target:Rebuild /Property:Configuration=Release;Platform=x64 /m /consoleloggerparameters:ErrorsOnly /fileLogger /fileloggerparameters:LogFile=""C:\Path\To\Project\SolutionName\BuildLog.txt"" /flp1:LogFile=""C:\Path\To\Project\SolutionName\BuildErrors.txt"";ErrorsOnly /flp2:LogFile=""C:\Path\To\Project\SolutionName\BuildWarnings.txt"";WarningsOnly

我已经无数次浏览错误中的链接(特别是与 MSBuild 有关的文档部分)。这让我在上面的命令中添加了一个额外的恢复目标(更改为 ../target:Restore;Rebuild ...)但是没有用(不记得确切的错误但我显然可以复制如果有人在阅读更多内容后认为它是相关的,请再次使用它)。我尝试了其他几次迭代,但我现在所处的位置,我认为这是执行此操作的正确方法,但它不起作用,所以这就是我来这里寻求帮助的原因。现在,我在上面的构建命令之前执行了以下命令:

msbuild "C:\Path\To\Project\SolutionName\SolutionFile.sln" /target:Restore /Property:Configuration=Release;Platform=x64

这个命令的输出是:

Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Framework

Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.

Build started 6/3/2020 1:02:06 AM.

Project "C:\Path\To\Project\SolutionName\SolutionFile.sln" on node 1 (Restore target(s)). ValidateSolutionConfiguration:

Building solution configuration "Release|x64". ValidateProjects:

The project "unrelated_project_in_solution_1" is not selected for building in solution configuration "Release|x64".

The project "unrelated_project_in_solution_2" is not selected for building in solution configuration "Release|x64".

The project "unrelated_project_in_solution_3" is not selected for building in solution configuration "Release|x64".

Restore:

Nothing to do. None of the projects specified contain packages to restore.

Done Building Project "C:\Path\To\Project\SolutionName\SolutionFile.sln" (Restore target(s)).

Build succeeded. 0 Warning(s) 0 Error(s)

Time Elapsed 00:00:01.99

当然(因为恢复没有做任何事情)接下来是项目无法构建,并出现与我添加命令之前相同的错误。

那么我在这里缺少什么?我的逻辑思维过程告诉我项目文件没有引用 nuget 包,但它似乎在那里(在 IDE 和 .vcxproj 文件中)所以我不知道为什么没有找到恢复命令

UPDATE1 - 好的,在通读 https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets 之后显然,msbuild 恢复选项仅适用于 PackageReference 样式的项目,默认情况下,VS2019 仍会为 C++ 项目创建 packages.config 样式的项目。

看来我需要将项目切换为使用 PackageReference,但我仍然不清楚如何执行此操作,因为 C++ 项目似乎根本不支持 PackageReference?或者它只是不适用于 C++ 项目的迁移工具? https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference

或者,我需要找到一种使用 package.config 样式项目自动管理安装的方法,但我也不知道该怎么做。

UPDATE2 - MSBuild 中的 native C++ 项目根本不支持 PackageReference。这里有一个正在进行的讨论(最后一次由 MSFT 代表于 2020 年 3 月更新)说他们将增加对 C++/Cli 的支持,但原生 C++ 需要做更多的工作。 https://developercommunity.visualstudio.com/idea/351636/use-packagereference-in-vcxproj.html 所以,我想我一直在 MSBuild 外部管理这个。

最佳答案

回到这里发布更新 2 作为答案,因为它现在仍然是最新的。

UPDATE2 - MSBuild 中的 native C++ 项目根本不支持 PackageReference。这里有一个正在进行的讨论(最后一次由 MSFT 代表于 2020 年 3 月更新)说他们将增加对 C++/Cli 的支持,但原生 C++ 需要做更多的工作。 https://developercommunity.visualstudio.com/idea/351636/use-packagereference-in-vcxproj.html所以,我想我一直在 MSBuild 外部管理这个。

关于visual-c++ - 如何让 MSBuild 自动下载解决方案/项目中引用的新 nuget 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62172582/

相关文章:

c# - 如何在 Visual Studio 中启用 NuGet 包还原?

f# - FSharp.Core 发行说明在哪里?

c++ - MKL 中的 cblas_dcopy 和 dcopy 之间的区别?

msbuild - 如何在 MSBuild 中编译 TypeLite T4 模板

azure - 如何使用 azure devOps 在 azure 应用服务上部署 nupkg 文件

msbuild - 以独立于平台的方式从 msbuild 项目运行 .net 可执行文件

visual-studio-2010 - 在 TFS2010 构建定义中指定构建配置时出现问题

c++ - 使用 Visual C++ 2010 嵌入 v8

python - 我在安装 pocketsphinx 时收到“python 3.6.1 需要错误 : Microsoft Visual C++ 14. 0”

c++ - 取消定义已经包含的标题