VS 2017 构建工具失败,出现错误 MSB4019:找不到导入的项目 "D:\Microsoft.Cpp.Default.props"

VS 2017 Build Tools failing with Error MSB4019: The imported project "D:\Microsoft.Cpp.Default.props" was not found

我正在构建一个新的 TFS 构建服务器并决定使用 VS 2017 构建工具而不是安装完整版本的 VS。当我尝试构建我们的 C++ 项目时,它抛出以下错误:

Error MSB4019: The imported project "D:\Microsoft.Cpp.Default.props" was not found.

经过几个小时的研究,我离解决这个问题还差得很远。我尝试添加以下注册表设置但没有帮助。

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0]
"VCTargetsPath"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\'))"
"VCTargetsPath11"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath11)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V110\'))"
"VCTargetsPath14"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath14)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\'))"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0.0]
"VCTargetsPath"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V110\'))"
"VCTargetsPath11"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath11)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V110\'))"

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions.0.0]
"VCTargetsPath"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\'))"
"VCTargetsPath11"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath11)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V110\'))"
"VCTargetsPath14"="$([MSBuild]::ValueOrDefault('$(VCTargetsPath14)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V140\'))"

我猜 Microsoft 的安装程序无法用于此产品。是否有针对此错误的标准修复程序,还是我应该放弃我的努力并简单地安装完整版本?

在项目文件中,我找到了这个条目:

<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />

所以,我想这意味着变量 VSTargetsPath 以某种方式指向 D: 驱动器的根目录,但我不知道该值的设置位置。我们当前的构建服务器没有名为 VSTargetsPath 的环境变量集,但它确实缺少注册表项。它还安装了完整版本的 VS。

VS 2017 Build Tools failing with Error MSB4019: The imported project “D:\Microsoft.Cpp.Default.props” was not found

尝试从构建配置中将 VCTargetsPath 作为 属性 显式传递给 msbuild:

  1. 编辑构建的构建定义。

  2. 单击进程选项卡。

  3. 在“高级”部分中,设置 MSBuild 参数以包括以下 属性:

    /p:VCTargetsPath="C:\Program Files (x86)\Microsoft Visual Studio17\xxx\Common7\IDE\VC\VCTargets\"

  4. 保存构建定义。

注意:您应该将 VCTargetsPath 的值更改为 VCTargets 文件夹的位置。

VisualStudioVersion作为属性传递给msbuild:/p:VisualStudioVersion=15.0

如果您对 $(VCTargetsPath) 的值感兴趣,可以查看以下主题以了解更多详细信息:

Can't find registry entries for Visual Studio 2017

Visual Studio Locator

Over the years Visual Studio could be discovered using registry keys, but with recent changes to the deployment and extensibility models a new method is needed to discover possibly more than once installed instance. These changes facilitate a smaller, faster default install complimented by on-demand install of other workloads and components.

vswhere is designed to be a redistributable, single-file executable that can be used in build or deployment scripts to find where Visual Studio - or other products in the Visual Studio family - is located. For example, if you know the relative path to MSBuild, you can find the root of the Visual Studio install and combine the paths to find what you need.

You can emit different formats for information based on what your scripts can consume, including plain text, JSON, and XML. Pull requests may be accepted for other common formats as well.

vswhere is included with the installer as of Visual Studio 2017 version 15.2 and later, and can be found at the following location: %ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe.