How to launch correct version of Msbuild - visual-studio-2010

When I type...
Msbuild<Enter>
...at the command prompt, I get...
Microsoft (R) Build Engine Version 2.0.50727.4927
[Microsoft .NET Framework, Version 2.0.50727.4927]
Copyright (C) Microsoft Corporation 2005. All rights reserved.
This is all very well and good except that when I run this against a Visual Studio 2010 .sln file, the error message indicates:
MyProject.sln(2): Solution file error MSB5014: File format version is not recognized. MSBuild can only read solution files between versions 7.0 and 9.0, inclusive.
0 Warning(s)
1 Error(s)
It would appear that the version of MSBuild that is being called, is not capable of understanding my solution file.
I figured that I would check out my path and see where MSBuild is being picked up from. However, it seems that no part of my path points at a location where MSBuild is to be found.
How is the command line finding the copy of MSBuild that it is using and how can I change this version so that the latest version is used?

I found this question as my PATH variable did not contain a reference to MSBuild.exe. In case anyone else is having this issue, my resolution was to explictly register the environment variables for Visual Studio tools from the command prompt:
"%VS100COMNTOOLS%"\\vsvars32.bat // VS2010 environment variables
"%VS110COMNTOOLS%"\\vsvars32.bat // VS2012 environment variables
"%VS120COMNTOOLS%"\\vsvars32.bat // VS2013 environment variables
MSBuild.exe is now registered in PATH
where msbuild
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe

It must be somewhere in the PATH environment. Use 'where msbuild' to determine where it is loading msbuild from.
Use the Visual Studio Command Prompt (2010) shortcut to initialize the path and other environment variables for VS 2010 and MSBuild 4.0.

Change your environment variables.
Find/set your MSBuild path variable to be C:\Windows\Microsoft.NET\Framework\v4.0.30319
Obiviously the above path will depend on your installation location, but it will be close if not exact.

For building a C# 6 project, this worked for me in a batch file
if exist "%ProgramFiles(x86)%\MSBuild\14.0\bin" set MSBUILDLOCATION=%ProgramFiles(x86)%\MSBuild\14.0\bin
if exist "%ProgramFiles%\MSBuild\14.0\bin" set MSBUILDLOCATION=%ProgramFiles%\MSBuild\14.0\bin
"%MSBUILDLOCATION%\msbuild.exe" "path\to\my\project.csproj"
You can easily change the version number (14.0) or add lines with more version numbers (12.0 and 4.0 for example) if you wanted to support a range of versions.

Related

Unable to build a freshly created Xamarin Cross-Platform project with MsBuild

Here the repro steps:
Start VS 2017
Create a cross-platform project ==> Sample.sln
Build it in VS ==> Ok
By building with it with the command "msbuild Sample.sln" I get 4 times the following error message: error MSB4066: The attribute "Version" in element "PackageReference" is unrecognized.
Anything missing in the .csproj files I am supposed to add?
Thanks in advance for your support.
Make sure that you use the path to msbuild.exe that is installed with Visual Studio. Use the Developer Command Prompt for Visual Studio 2017 if unsure, it has the PATH set up so that msbuild will point to the right executable.
This error happy when you use an old version of MSBuild (e.g. the version included in .NET Framework) that doesn't support the necessary features - metadata as attributes in this case.

msbuild fails in Teamcity

I might be missing something really simple here. I'm trying to build a solution in Teamcity using msbuild but it always keeps failing with the below error.
'msbuild' is not recognized as an internal or external command,
operable program or batch file.
I have VS2017 installed and when I run the solution locally through command line on the same Teamcity agent, it works perfectly with the following comments.
Microsoft (R) Build Engine version 15.4.8.50001 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
But for some reason it always fails on teamcity. I'm at a loss as to what am I missing here. Any pointers are greatly appreciated.
Following is the command that I'm using to compile the solution
msbuild hello.sln /t:Rebuild
msbuild fails in Teamcity
Agree with stijn. According to the error log, it seems MSBuild not in the default path, so it could not be found.
To resolve this issue, you should make sure you have installed the Visual Studio 2017 or Microsoft Build Tools on the teamcity agent and need to properly configure your build runner, especially MSBuild version.
Besides, you can also try to create environment variable "MSBuild" and set it to the path of MSBuild.exe in your teamcity agent.
The default path of MSBuild for Visual Studio 2017 is:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin
If above not help you, please share us your configuration about Build Runner.

VS 2013 and MSBuild

I've recently upgraded to Visual Studio 2013, which has caused back to back problems when building externally using MSBuild (API or Executable with command line args)
Issue #1
When building with MSBuild it doesn't generate Fake assemblies which are required for our Unit Tests, this leads to build failures. A simple build in visual studio fixes this temporarily, until a new fake assembly needs to be generated.
Issue #2
When running code analysis this complains with the following:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets(284,5):
error MSB4127: The "CodeAnalysis" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\.\FxCopTask.dll".
Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.CodeAnalysis' to type 'Microsoft.Build.Framework.ITask'.
I only have Visual Studio 2013 installed on my machine, apparently installing an older version could fix the issue, but it's not something which I can do. (VS 2013 Ships with its own MSBuild 12.0 which is located in a different directory to the previous MSBuild).
I'm unsure why Visual Studio is behaving any differently to MSBuild, i'm simply pointing to the solution file like so...
msbuild.exe "path\solution.sln" /property:Configuration=Debug
So, Visual Studio 2013 comes with a new version of MSBuild i.e. MSBuild 12.0. Once installed, it changes the path so that the new version is used by default.
Looks like your solution compiles with visual studio 2012, you can either specify the full path to msbuild.exe such as
C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "path\solution.sln" /property:Configuration=Debug
or set the visual studio 2012 environment variables by running the following before executing msbuild
"%VS110COMNTOOLS%"\vsvars32.bat // VS2012 environment variables
EDIT: Using MSbuild 12.0 assemblies "C:\Program Files (x86)\MSBuild\12.0\Bin\" fixes the issue with the code Analysis bug.

Platform toolset (v110) is not installed or invalid

I have a weird problem when I want to compile a Visual Studio 2012 solution via msbuild on the command line. Whatever I do, it exits with Specified platform toolset (v110) is not installed or invalid. I have tried launching it via the regular command prompt, the Windows 7 SDK prompt, and all three command prompts included in VS2012. However, compiling in Visual Studio itself works.
where msbuild outputs:
c:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe
c:\Windows\Microsoft.NET\Framework64\v3.5\MSBuild.exe
You have probably solved the problem yourself but perhaps it may help others with a similar problem. Try to set the VisualStudioVersion environment variable before running MSBuild, e.g
SET VisualStudioVersion=11.0
There is a possibility that it helps.
You could also try passing the command line parameter /p:VisualStudioVersion=11.0 to MSBuild, when trying to build your project. It seems to have the same effect as setting the environment variable, in the above answer.

nmake, visualstudio, and .mak files

I was given a C++ project that was compiled using MS Visual Studio .net 2003 C++ compiler, and a .mak file that was used to compile it. I am able to build it from the command line using nmake project.mak, but the compiler complains that afxres.h was not found. I did a little searching around and the afxres.h is in the Visual Studio directory in an includes file. Where am I supposed to specify to nmake where to look for this header file?
There should be an icon in your Start menu under Programs that opens a cmd.exe instance with all the correct MSVS environment variables set up for command line building.
Another option is running the appropriate vars batch file from a regular command prompt. The name and location varies from version to version. For VS2003, I believe it's
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\vsvars32.bat

Resources