简体   繁体   English

如何从 BAT 文件调用 Visual Studio 2017 RC 版本的 MSBuild?

[英]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.可以在此处找到早期版本的 MSBuild: %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 .但是对于 Visual Studio 2017RC,路径是%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.这使得很难在 BAT 文件中指定正确的 msbuild 路径,该文件应该适用于具有不同 Visual Studio 2017 版本的许多不同计算机。

What is the best way to call Visual Studio 2017 RC's version of Msbuild from a bat file?从 bat 文件调用 Visual Studio 2017 RC 版本的 Msbuild 的最佳方法是什么? Or from PowerShell?还是来自 PowerShell?

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. VS2017 的安装引擎有一个互操作 API,允许您查询有关安装了哪些 VS2017 实例的信息。 There's a NuGet package for it, and even a sample of how to query, including the installation path.它有一个NuGet 包,甚至还有一个如何查询的示例,包括安装路径。 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.对于批处理或 PS 文件中的使用,您可能只需重写示例应用程序并从您的脚本中调用它以输出您需要的信息。

The unsupported way不支持的方式

The VS setup engine keeps a store of its data under %ProgramData%\\Microsoft\\VisualStudio\\Packages\\_Instances . VS 安装引擎将其数据存储在%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.每个 VS2017 实例都有一个文件夹,该文件夹内是一个state.json文件,其中包含有关该安装的信息,包括安装路径。

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.由于您需要从 .json 文件中提取信息,您可以选择编写从脚本文件调用的应用程序,或者直接在脚本中提出一些解析逻辑。 This will obviously be fragile as the JSON schema or file location may change.这显然是脆弱的,因为 JSON 模式或文件位置可能会改变。

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).假设您使用的是默认安装路径,您可以在%ProgramFiles(x86)%\\Microsoft Visual Studio\\下递归搜索 msbuild.exe(似乎每个 VS 都会有 32 位和 64 位 msbuild.exe例)。 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.您可以做的最后一件事是要求开发人员使用(或以某种方式调用) vsdevcmd.bat以使用 VS 开发环境。 This will get them MSBuild, and additionally any other tools from the VS environment, onto their %PATH%.这将使他们获得 MSBuild,以及来自 VS 环境的任何其他工具,进入他们的 %PATH%。 This does place a requirement on your dev team, but will always be an officially supported way to find msbuild.exe.这确实对您的开发团队提出了要求,但始终是官方支持的查找 msbuild.exe 的方式。

Microsoft have created a tool to find the location of Visual Studio 2017 and newer https://github.com/Microsoft/vswhere微软已经创建了一个工具来查找 Visual Studio 2017 和更新版本的位置https://github.com/Microsoft/vswhere

That option and newer are decribed in this blog post. 博客文章中描述了选项和更新的选项。

Simplest way...最简单的方法...

就在 VS 下载页面的底部

  • Profit;利润; the build tools will install to a separate location thats standard across everywhere you install it regardless of your VS SKU (professional / enterprise / community all install to different paths)无论您的 VS SKU 是什么,构建工具都将安装到一个单独的位置,这是您安装它的任何地方的标准(专业/企业/社区都安装到不同的路径)
  • New location C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\msbuild.exe新位置C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\msbuild.exe

Posting this for folks still trying to locate the newest version of msbuild on a machine and fall back to an older one if that is availble instead.为那些仍然试图在机器上找到最新版本的 msbuild 并回退到旧版本(如果可用)的人们发布此信息。 I find this snippet of powershell does the trick, which can easily be invoked from a bat file .我发现这个 powershell 片段可以解决问题,它可以很容易地从 bat 文件中调用

Function Find-MsBuild([int] $MaxVersion = 2017)
{
    $agentPath = "$Env:programfiles (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\msbuild.exe"
    $devPath = "$Env:programfiles (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\msbuild.exe"
    $proPath = "$Env:programfiles (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\msbuild.exe"
    $communityPath = "$Env:programfiles (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"
    $fallback2015Path = "${Env:ProgramFiles(x86)}\MSBuild\14.0\Bin\MSBuild.exe"
    $fallback2013Path = "${Env:ProgramFiles(x86)}\MSBuild\12.0\Bin\MSBuild.exe"
    $fallbackPath = "C:\Windows\Microsoft.NET\Framework\v4.0.30319"

    If ((2017 -le $MaxVersion) -And (Test-Path $agentPath)) { return $agentPath } 
    If ((2017 -le $MaxVersion) -And (Test-Path $devPath)) { return $devPath } 
    If ((2017 -le $MaxVersion) -And (Test-Path $proPath)) { return $proPath } 
    If ((2017 -le $MaxVersion) -And (Test-Path $communityPath)) { return $communityPath } 
    If ((2015 -le $MaxVersion) -And (Test-Path $fallback2015Path)) { return $fallback2015Path } 
    If ((2013 -le $MaxVersion) -And (Test-Path $fallback2013Path)) { return $fallback2013Path } 
    If (Test-Path $fallbackPath) { return $fallbackPath } 

    throw "Yikes - Unable to find msbuild"
}


$msbuildPath = Find-MsBuild 2017

Now that VS 2017 has been released, thought it might be useful to add my 2 cents:既然 VS 2017 已经发布,我认为添加我的 2 美分可能有用:

Creating a batch file:创建批处理文件:

Open Notepad打开记事本

Copy the following and paste into notepad, replacing the solution you want to build with [My Solution Name Here]:复制以下内容并粘贴到记事本中,将您要构建的解决方案替换为 [My Solution Name Here]:

@echo off

:variables
SET msBuildLocation="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"
SET solutionDirectory=[My Solution Location Here]
SET batchFileLocation=[BatchFileLocation Here]

cd %solutionDirectory%

echo Building MS files for [My Solution Name Here].

call %msBuildLocation% [My Solution Name].sln /p:Configuration=Debug /m:4 /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:false  /consoleloggerparameters:Summary;ShowTimestamp;ShowEventId;PerformanceSummary
echo -
echo --------Done building [My solution name here].--------------
echo -

cd %batchFileLocation%

Save your batch file as whatever you want with the .bat extension.使用 .bat 扩展名将您的批处理文件另存为您想要的任何文件。 Be sure to select all files instead of .txt or else it won't work.请务必选择所有文件而不是 .txt,否则将无法使用。 Call the batch file by issuing the cd command where you have it saved at, and it should work.通过发出 cd 命令来调用批处理文件,它应该可以工作。 Or, just double-click.或者,只需双击即可。 Done.完成。

Please note that my version of visual studio is community;请注意,我的visual studio 版本是community; you'll need to replace the path with the appropriate version.您需要用适当的版本替换路径。

I also run the build tools from a bat file on my CI server: The new location I've found is this:我还从 CI 服务器上的 bat 文件运行构建工具:我找到的新位置是这样的:

%programfiles(x86)%\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\msbuild.exe %programfiles(x86)%\\Microsoft Visual Studio\\2017\\BuildTools\\MSBuild\\15.0\\Bin\\msbuild.exe

If you have installed the .NET Core tools preview 3 or later, the dotnet CLI should be available on the %PATH% by default (even if you're not building a .NET Core project), so you could try dotnet msbuild .如果您已安装 .NET Core 工具预览版 3 或更高版本,默认情况下dotnet CLI 应该在%PATH%上可用(即使您没有构建 .NET Core 项目),因此您可以尝试dotnet msbuild Ref https://github.com/dotnet/docs/blob/master/docs/core/preview3/tools/dotnet-msbuild.md参考https://github.com/dotnet/docs/blob/master/docs/core/preview3/tools/dotnet-msbuild.md

I maintain a very simple PowerShell script ( gist ) to run CI/CLI builds with properly configured Visual Studio environment variables.我维护了一个非常简单的 PowerShell 脚本 ( gist ) 来运行带有正确配置的 Visual Studio 环境变量的 CI/CLI 构建。 It uses vswhere.exe and VsDevCmd.bat .它使用vswhere.exeVsDevCmd.bat It works well on build agents as a part of Azure DevOps pipelines.作为 Azure DevOps 管道的一部分,它在构建代理上运行良好。 Particularly for C++ projects, it's vital to set INCLUDE and LIB properly, that's what VsDevCmd.bat does.特别是对于 C++ 项目,正确设置INCLUDELIB至关重要,这就是VsDevCmd.bat所做的。

Eg, run a build with MSBuild for the solution in the current folder:例如,使用MSBuild为当前文件夹中的解决方案运行构建:

powershell -f _invoke-build.ps1 -buildCommand "msbuild ."

Copy VS' INCLUDE environment variable into clipboard (for fun):将 VS 的INCLUDE环境变量复制到剪贴板(为了好玩):

powershell -f _invoke-build.ps1 -buildCommand "set INCLUDE | clip"

The current version of the script:脚本的当前版本:

<#
  .Synopsis
    Find Visual Studio and run its VsDevCmd.bat, then run a build command.

  .Example
    powershell -f _invoke-build.ps1 -buildCommand "msbuild ."

  .Link
    https://gist.github.com/noseratio/843bb4d9c410c42081fac9d8b7a33b5e
#>

#Requires -Version 5.0
# by @noseratio

param([Parameter(Mandatory = $true)] [string] $buildCommand)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

echo "Building via '$buildCommand' ..."

$vs_not_found = "Visual Studio hasn't been detected!"

$vswhere = "${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe"
if (!(Test-Path $vswhere)) { throw $vs_not_found }

$vs_ide_path = $(& $vswhere -format value -property productPath)
if (!(Test-Path $vs_ide_path)) { throw $vs_not_found }

$vs_ide_folder = "$(Split-Path $vs_ide_path)"
$vs_dev_cmd = "$vs_ide_folder/../Tools/VsDevCmd.bat"
$invoke = "call ""$vs_dev_cmd"" && cd ""$pwd"" && $buildCommand"

# we need to run via CMD for proper propagation of Visual Studio environment vars
& $env:comspec /s /c ""$invoke""
if (!$?) { throw "Failed with error code: $LastExitCode" }

SO I have Community edition 2017, for me path is: C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\所以我有社区版 2017,对我来说路径是:C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\

Other solutions on this thread throwed me an exception.此线程上的其他解决方案使我异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Visual Studio 2017 RC中的.NET Core MSBuild项目创建NuGet包? - How do you create a NuGet package from a .NET Core MSBuild project in Visual Studio 2017 RC? 如何使用Visual Studio 2012中的MSBuild文件? - How do I use an MSBuild file from Visual Studio 2012? 如何使Visual Studio 2017 / msbuild使用最新的DirectX着色器编译器? - How do I get Visual Studio 2017 / msbuild to use the latest DirectX shader compiler? 如何控制在.NET4和4.5RC之间使用哪个版本的msbuild文件? - How do I control which version of an msbuild file is used between .NET4 and 4.5RC? 如何从Visual Studio宏获取msbuild路径? - How do I get the msbuild path from Visual Studio macro? 我如何阻止 Visual Studio 2017 在保存时构建 - How do i stop visual studio 2017 from building on save MSBuild 创建BAT文件,调用后立即删除,如何查看BAT内容? - MSBuild creates BAT file, call it and delete it immediately, how can I see BAT content? Visual Studio 2017 MSBuild问题 - Visual Studio 2017 MSBuild issue 添加在Visual Studio 2017 RC中构建.NET Core项目后运行的msbuild任务 - Add a msbuild task that runs after building a .NET Core project in Visual Studio 2017 RC 如何让Visual Studio 2010将变量传递给MSBuild? - How do I get Visual Studio 2010 to pass a variable to MSBuild?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM