在构建服务器上找不到 Microsoft.WebApplication.targets 文件。你有什么解决方法?

471

尝试在构建服务器上构建我的项目时,出现以下错误:

Microsoft (R) Build Engine Version 4.0.30319.1
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\TeamData\Microsoft.Data.Schema.SqlTasks.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

几个月前,我通过在Build Server上安装Visual Studio 2010来解决了这个问题。但现在我要从头开始设置一个新的服务器,想知道是否有更好的解决方案来解决这个问题。


1
Web应用程序项目已经过时了吗?我想知道为什么需要旧版本的Visual Studio才能构建它们的理由是什么? - brianary
1
更重要的是,你是否实际通过构建服务器进行部署?例如,我不这样做,我甚至在解决方案中有一个单独的Web安装程序项目...但它仍然需要这个该死的东西...答案=从proj文件中删除它!很容易。 - Paul Zahra
8
通过将路径<Import Project="..\Packages\MSBuild.Microsoft.VisualStudio.Web.targets.14.0.0.3\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" />替换为$(VSToolsPath),完成修复操作,修复后的代码如下: <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" /> - G J
24个回答

10

似乎新版本的msbuild没有包含Microsoft.WebApplication.targets。要修复它,您需要按以下方式更新csproj文件:

1)编辑Web应用程序csproj(右键单击)。找到有关构建工具的csproj底部部分。它应该是这样的。

<PropertyGroup>  
  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
</PropertyGroup>  
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />  
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />  
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />  

2) 你需要在VisualStudioVersion标记下方添加一行VSToolsPath以使其看起来像这样

<PropertyGroup>  
  <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
  <!--Add the below line to fix the project loading in VS 2017 -->
  <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  <!--End -->
</PropertyGroup>  
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />  
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />  
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />  

参考链接: https://alastaircrabtree.com/cannot-open-vs-2015-web-project-in-vs-2017/


8

这就是您需要的全部内容。只有103MB。不要安装所有内容。

输入图像说明


我如何能在那个表格上打上勾,而不需要安装任何东西? - Christian

6

我在MS connect上找到了这个:

是的,你需要在构建机器上安装Visual Studio 2010来构建数据库项目。这样做不需要额外购买Visual Studio的许可证。

所以,现在这是我唯一的选择。


3
链接似乎已经失效。 - Disillusioned

4

如果您在使用Visual Studio 2017时遇到问题,更新至15.6.1版本后无法编译项目,那么您可能需要安装MSBuild工具。但即使这样,错误仍然存在。

您可以通过将来自“C:\ Program Files(x86)\ MSBuild \ Microsoft \ VisualStudio”的v14.0文件夹复制到与v15.0相同的文件夹中来解决此问题。这样,现在我的文件夹结构如下,两个文件夹都包含相同的内容。

enter image description here


4
如果您正在使用MSBuild,例如在构建服务器上,则以下方法适用于我:
更改以下内容:
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />

收件人:

<Import Project="$(MSBuildBinPath)\Microsoft.VisualBasic.targets" />
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />

我的Msbuild命令如下:*"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" solution.sln /p:Configuration=Debug /p:Platform="Any CPU"* 希望这能帮到大家。

需要提到的是,更改应该针对有问题的.csproj、vbproj文件进行。 - Colin Q

2
我通过添加以下内容来解决这个问题:
/p:VCTargetsPath="C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\V120"

在以下位置插入:
生成 > 使用 MSBuild 构建 Visual Studio 项目或解决方案 > 命令行参数

2
我的解决方案是混合了几个答案。
我检查了构建服务器,发现已安装Windows7/NET4.0 SDK,所以我找到了路径:
``` C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets ```
然而,在这行代码中:
``` ```
`$(MSBuildExtensionsPath)`扩展为`C:\Program Files\MSBuild`,没有这个路径。
因此,我创建了一个符号链接,使用以下命令:
``` mklink /J "C:\Program Files\MSBuild\Microsoft\VisualStudio" "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio" ```
这样,`$(MSBuildExtensionsPath)`扩展为有效路径,并且不需要在应用程序本身中进行任何更改,只需要在构建服务器上进行更改(也许可以在每次构建时创建符号链接,以确保此步骤不会丢失并且是“记录的”)。

2

我尝试过很多解决方案,但最终这个答案对我有用:https://dev59.com/t2Qm5IYBdhLWcg3wqwfn#19826448

基本上就是从MSBuild目录调用MSBuild,而不是从Visual Studio目录调用。

我还将MSBuild目录添加到了我的路径中,以便更轻松地编写脚本。


1
我在CI/CD流水线上构建SQL Server项目时遇到了问题。实际上,我在本地也遇到了同样的问题,但未能解决。
对我有用的是使用MSBuild SDK,它能够从一组SQL脚本生成SQL Server数据层应用程序包(.dacpac),这意味着需要创建一个新项目。但我想保留SQL Server项目,以便可以通过Visual Studio上的SQL Server对象浏览器将其链接到实时数据库。我采取了以下步骤来使其正常运行:
  1. Kept my SQL Server project with the .sql database scripts.
  2. Created a .NET Standard 2.0 class library project, making sure that the target framework was .NET Standard 2.0, as per the guidelines in the above link.
  3. Set the contents of the .csproj as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <Project Sdk="MSBuild.Sdk.SqlProj/1.0.0">
      <PropertyGroup>
        <SqlServerVersion>Sql140</SqlServerVersion>
        <TargetFramework>netstandard2.0</TargetFramework>
      </PropertyGroup>
    </Project>
    
  4. I have chosen Sql140 as the SQL Server version because I am using SQL Server 2019. Check this answer to find out the mapping to the version you are using.

  5. Ignore the SQL Server project on build, so that it stops breaking locally (it does build on Visual Studio, but it fails on VS Code).

  6. Now we just have to make sure the .sql files are inside the SDK project when it is built. I achieved that with a simple powershell routine on the CI/CD pipeline that would copy the files from the SQL Server project to the SDK project:

复制-项目路径 "Path.To.The.Database.Project\dbo\Tables\*" -目标 (New-item -Name "dbo\Tables" -Type Directory -Path "Path.To.The.DatabaseSDK.Project\")

PS:这些文件必须实际存在于SDK项目中,可以位于根目录或某个文件夹中,因此在SQL Server项目中链接到.sdk文件是行不通的。理论上,可以通过预构建条件复制这些文件,但由于某种不明原因,这对我来说并不起作用。我还尝试将.sql文件放在SDK项目中并将其链接到SQL Server项目,但这很容易破坏与SQL Server对象资源管理器的链接,所以我决定放弃这个方法。


0

创建一个新项目并复制设置可能是最好的指导。这是我的界面

  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接