Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call Visual Studio 2017 RC's version of MSBuild from a BAT file?

Earlier versions of MSBuild could be found here: %programfiles(x86)%\msbuild\<version>\bin\msbuild.exe.

But for Visual Studio 2017RC the path is %programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\msbuild.exe. The type of install: enteprise, community or professional, seems to be part of the path. And that makes it difficult to specify the correct path to msbuild in a BAT file that should work on many different computers with different versions of Visual Studio 2017.

What is the best way to call Visual Studio 2017 RC's version of Msbuild from a bat file? Or from PowerShell?

like image 360
Arve Avatar asked Nov 19 '16 15:11

Arve


People also ask

Does Visual Studio call MSBuild?

Visual Studio uses MSBuild, but MSBuild doesn't depend on Visual Studio. By invoking msbuild.exe on your project or solution file, you can orchestrate and build products in environments where Visual Studio isn't installed.

What version of MSBuild does vs2022 use?

Visual Studio 2022 uses the 64-bit version of MSBuild for all builds.


1 Answers

The supported way

VS2017's setup engine has an interop API that allows you to query information about which instance(s) of VS2017 are installed. There's a NuGet package for it, and even a sample of how to query, including the installation path. For consumption in a batch or PS file, you might just rewrite the sample app and call it from your script to output the information you need.

The unsupported way

The VS setup engine keeps a store of its data under %ProgramData%\Microsoft\VisualStudio\Packages\_Instances. There will be a folder for each instance of VS2017, and inside that folder is a state.json file that contains information about that installation, including the install path.

Since you need to extract the info from the .json file, you have the option to either write an app that you call from your script file, or come up with some parsing logic directly in your script. This will obviously be fragile as the JSON schema or file location may change.

The brute force method

Assuming you are using the default installation path, you could just recursively search for msbuild.exe under %ProgramFiles(x86)%\Microsoft Visual Studio\ (it appears that there will be 32-bit and 64-bit msbuild.exe for each VS instance). This would probably be the easiest to do within your script file, but does rely on the default installation path (or whatever hardcoded path you wish to search under).

Change your dev environment requirements

The last thing you could do is require that devs use (or somehow invoke) the vsdevcmd.bat to use the VS dev environment. This will get them MSBuild, and additionally any other tools from the VS environment, onto their %PATH%. This does place a requirement on your dev team, but will always be an officially supported way to find msbuild.exe.

like image 101
Jimmy Avatar answered Sep 19 '22 13:09

Jimmy