使用 MSBuild 通过 VS2017 构建 SSDT 项目失败

Using MSBuild to build SSDT projects with VS2017 fails

我正在尝试在我们的 windows 服务器 2016 服务器上设置 .sqlproj SSDT 项目的持续集成。

为了做到这一点,我在服务器上安装了 VS2017 和 SSDT 工具以获取所需的工具。我还安装了 MS 构建工具 2017。

我目前遇到的问题是安装的 MS 构建工具和 ssdt 的版本似乎不匹配。

我用来运行构建的命令如下:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe /p:VisualStudioVersion=15.0 /property:MSBuildExtensionsPath="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\" /property:VsInstallRoot="C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\" DPAP-SQL-Slim.sln

那里传递的两个开关用于设置 .sqlproj 文件期望在 运行 到 Visual studio 时找到的两个环境参数。这是因为 MSBuild 默认不提供它们。

我目前得到的错误如下:

C:\Program Files (x86)\Microsoft Visual Studio17\Professional\MSBuild\Microsoft\VisualStudio\v15.0\SSDT\Microsoft.Data.Tools.Schema.SqlTasks.targets(477,5): error MSB4062: The "SqlModelResolutionTask" task could not be loaded from the assembly C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\SQLDB\Dac0\Microsoft.Data.Tools.Schema.Tasks.Sql.dll. Could not load file or assembly 'Microsoft.Build.Utilities.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

现在,我可以从 运行ning 命令中看到:gacutil /l 在 VS2017 命令提示符中显然安装的 Microsoft.Build.Utilities.Core dll 版本是 v14:Microsoft.Build.Conversion.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL

我是不是漏掉了什么关键的东西?似乎安装构建工具 2017 实际上并没有更新 GAC 并注册正确的 DLL。

不要使用 .NET Framework (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe) 中包含的 msbuild.exe。它很旧,不支持较新 msbuild 版本的某些功能。

始终使用随 visual studio 或构建工具安装的 MSBuild 版本。根据安装的 VS 版本,这可能是

C:\Program Files (x86)\Microsoft Visual Studio17\Enterprise\MSBuild.0\Bin\MSBuild.exe

从 VS 2017 开始,MSBuild 也不会在系统范围内安装,但可以并排安装许多版本(例如 VS 2017 预览版、构建工具/企业/社区等不同版本),这这就是您在 GAC 中找不到这些 MSBuild 程序集的原因。

我在 CI/CD 管道上构建 SQL 服务器项目时遇到了这个问题。 None Azure DevOps 上的预建构建任务对我有用。我通过避免向解决方案中添加 SQL 服务器项目来解决此问题。

我通过使用 MSBuild SDK, capable of producing a SQL Server Data-Tier Application package (.dacpac) from the set of SQL scripts. By adding this second project to the solution, I managed to continue taking advantage of linking the project to a live database through SQL Server Object Explorer on Visual Studio. I gave a more detailed explanation in this answer.

实现了这一点