4

使用 Powershell 中的 MSBuild 构建包含 Web 应用程序项目的解决方案,如下所示:

msbuild "/p:OutDir=$build_dir\" $solution_file

在 32 位上工作正常,但在 64 位机器上我遇到了这个错误:

错误 MSB4019:未找到导入的项目“C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets”。确认声明中的路径正确,并且该文件存在于磁盘上。

我正在使用 Visual Studio 2008 和 powershell v2。该问题已在此处此处记录。基本上在 VS 的 64 位安装上,MSBuild 所需的 Microsoft.WebApplication.targets 位于 Program Files(x86) 目录中,而不是 Program Files 目录中,但 MSBuild 无法识别这一点,因此看起来位置错误。

到目前为止,我的两个解决方案并不理想:

  1. 手动将 64 位文件从 Program Files(x86) 复制到 Program Files。这是一个糟糕的解决方案 - 每个开发人员都必须手动执行此操作。
  2. 手动编辑 csproj 文件,以便 MSBuild 位于正确的位置。同样不理想:我宁愿不必让每个人都使用 64 位来手动编辑每个新项目的 csproj 文件。

例如

<Import Project="$(MSBuildExtensionsPathx86)\$(WebAppTargetsSuffix)" Condition="Exists('$(MSBuildExtensionsPathx86)\$(WebAppTargetsSuffix)')" />

理想情况下,我想要一种方法来告诉 MSBuild 从命令行从正确的位置导入目标文件,但我不知道该怎么做。有什么解决办法吗?

4

2 回答 2

2

您还有其他几种选择:

由于 MSBuild 基本上可以使用环境变量,因此您始终可以在启动 msbuild 之前将 Program Files 更改为 ProgramFiles (x86)。

$env:ProgramFiles = ${env:ProgramFiles(x86)}

这应该会欺骗 MSBuild 寻找正确的位置

我能想到的另一种方法是使用 Start-Job 从脚本中运行 MSBuild。这更像是一种通用的解决方法:

Start-Job {
   Import-Module YourProject
   Start-YourMsBuildProject
} -RunAs32

希望这可以帮助

于 2010-03-26T19:06:47.440 回答
1

如果您从 32 位提示符运行 msbuild,这应该可以在 64 位计算机上无需任何修改即可工作。鉴于此测试文件:

<Project 
   DefaultTargets="Test" 
   xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
   ToolsVersion="3.5">

  <Target Name="Test">
    <Message text="$(MSBuildExtensionsPath)"/>
  </Target>
</Project>

我从我的 x64 系统上的 32 位 PowerShell 提示符得到这个结果:

PS> msbuild test.proj /nologo
Build started 3/26/2010 9:13:10 AM.
Project "C:\temp\test.proj" on node 0 (default targets).
  C:\Program Files (x86)\MSBuild
Done Building Project "C:\temp\test.proj" (default targets).

如果您直接从 VS 运行,这也应该不是问题,因为 VS 是一个 32 位应用程序。

于 2010-03-26T15:14:17.507 回答