Building Web Application project using MSBuild from command line on 64-bit: missing targets file

Building a solution containing a web application project using MSBuild from powershell like this:

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

Works fine for me on 32-bit, but on a 64-bit machine I am running into this error:

error MSB4019: The imported project "C:\Program Files\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

I am using Visual Studio 2008 and powershell v2. The problem has already been documented here and here. Basically on 64-bit install of VS, the Microsoft.WebApplication.targets needed by MSBuild is in the Program Files(x86) dir, not the Program Files dir, but MSBuild doesn't recognise this and so looks in the wrong place.

The two solutions I have so far are not ideal:

  1. Manually copy the file on 64-bit from Program Files(x86) to Program Files. This is a poor solution - every dev will have to do this manually.
  2. Manually edit the csproj file so MSBuild looks in the right place. Again not ideal: I would rather not have to get everyone on 64bit to manually edit csproj files on every new project.

e.g.

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

Ideally I want a way to tell MSBuild to import the target file form the right place from the command line but I can't work out how to do that. Any solutions?

Answers


This should work without any modifications on a 64 bit machine IF you run msbuild from a 32-bit prompt. Given this test file:

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

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

I get this result from a 32-bit PowerShell prompt on my x64 system:

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).

This also shouldn't be a problem if you run directly from VS because VS is a 32-bit app.


You have a couple of other alternatives:

Since MSBuild basically works off of environmental variables, you could always just change Program Files to be ProgramFiles (x86) before you start msbuild.

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

This should trick MSBuild into looking in the right place

The other approach I can think of is using Start-Job to run MSBuild from within the script. This is more of a general purpose workaround:

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

Hope this helps


Need Your Help

Missing method exception

grails groovy integration-tests

I was working on integration test and I am getting missing method exception every time,