如何称呼Visual Studio 2017 RC';BAT文件中的MSBuild的s版本?

如何称呼Visual Studio 2017 RC';BAT文件中的MSBuild的s版本?,msbuild,visual-studio-2017,Msbuild,Visual Studio 2017,可以在此处找到MSBuild的早期版本:%programfiles(x86)%\MSBuild\\bin\MSBuild.exe。 但对于Visual Studio 2017RC,路径是%programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe。安装类型:企业、社区或专业,似乎是路径的一部分。这使得在BAT文件中指定msbuild的正确路径变得非常困难,而BAT文件应该可以在许多

可以在此处找到MSBuild的早期版本:
%programfiles(x86)%\MSBuild\\bin\MSBuild.exe。

但对于Visual Studio 2017RC,路径是
%programfiles(x86)%\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe
。安装类型:企业、社区或专业,似乎是路径的一部分。这使得在BAT文件中指定msbuild的正确路径变得非常困难,而BAT文件应该可以在许多具有不同版本Visual Studio 2017的不同计算机上工作

从bat文件调用Visual Studio 2017 RC版本的Msbuild的最佳方法是什么?还是从PowerShell?

支持的方式 VS2017的安装引擎有一个互操作API,允许您查询有关已安装VS2017实例的信息。对于它,甚至对于如何查询,包括安装路径,都有一个详细的说明。对于批处理或PS文件中的消费,您可以重写示例应用程序,并从脚本中调用它以输出所需的信息

支路 VS安装引擎将其数据存储在
%ProgramData%\Microsoft\VisualStudio\Packages\\u实例下。VS2017的每个实例都有一个文件夹,该文件夹中有一个
state.json
文件,其中包含有关该安装的信息,包括安装路径

因为您需要从.json文件中提取信息,所以您可以选择编写一个从脚本文件调用的应用程序,或者直接在脚本中生成一些解析逻辑。这显然是脆弱的,因为JSON模式或文件位置可能会改变

蛮力法 假设您使用的是默认安装路径,则可以在
%ProgramFiles(x86)%\Microsoft Visual Studio\
下递归搜索msbuild.exe(每个VS实例似乎都有32位和64位msbuild.exe)。这可能是在脚本文件中最容易做到的,但依赖于默认安装路径(或您希望在其中搜索的硬编码路径)

更改您的开发环境需求
您可以做的最后一件事是要求开发人员使用(或以某种方式调用)vsdevcmd.bat来使用VS开发环境。这将使他们将MSBuild以及VS环境中的任何其他工具放到他们的%PATH%上。这确实对开发团队提出了要求,但始终是官方支持的查找msbuild.exe的方法。

如果您安装了.NET Core tools preview 3或更高版本,默认情况下,
dotnet
CLI应该在
%PATH%
上可用(即使您没有构建.NET Core项目),因此,您可以尝试
dotnetmsbuild
。Ref

微软已经创建了一个工具来查找Visual Studio 2017及更新版本的位置


该选项和更新的选项在博客文章中有所描述。

既然VS 2017已经发布,我想添加我的2美分可能会有用:

创建批处理文件:

打开记事本

复制以下内容并粘贴到记事本中,将要生成的解决方案替换为[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%
使用.bat扩展名将批处理文件保存为所需的任何文件。确保选择所有文件而不是.txt,否则它将无法工作。 通过在保存批处理文件的位置发出cd命令来调用该批处理文件,该批处理文件应该可以工作。或者,只需双击。完成了


请注意,我的visual studio版本是社区版;您需要将路径替换为适当的版本。

我还从CI服务器上的bat文件运行生成工具:我找到的新位置如下:

%程序文件(x86)%\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe

最简单的方法

  • 安装构建工具

  • 利润;构建工具将安装到一个单独的位置,该位置在您安装它的任何地方都是标准的,无论您的VS SKU如何(专业/企业/社区都安装到不同的路径)
  • 新位置
    C:\ProgramFiles(x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe

为仍在试图在计算机上查找最新版本的msbuild的用户发布此消息,如果可用,则返回到旧版本。我发现powershell的这段代码很好地实现了这一点,这很容易实现


因此,我有2017社区版,对我来说,路径是: C:\ProgramFiles(x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\


这个线程上的其他解决方案给我带来了一个例外。

我维护了一个非常简单的PowerShell脚本(),用正确配置的Visual Studio环境变量运行CI/CLI构建。它使用
vswhere.exe
VsDevCmd.bat
。作为Azure DevOps管道的一部分,它在构建代理上运行良好。特别是对于C++项目,设置<代码>包含和 LIB <代码>是至关重要的,这就是<代码> VsDevCmd。BAT 例如,使用
MSBuild
为当前文件夹中的解决方案运行生成:

powershell -f _invoke-build.ps1 -buildCommand "msbuild ."
将VS'
包含
环境变量复制到剪贴板中(为了好玩):

脚本的当前版本:

<#
  .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" }


#需要-版本5.0
#按比例
param([Parameter(Mandatory=$true)][string]$buildCommand)
设置StrictMode-最新版本
$ErrorActionPreference='Stop'
echo“通过“$buildCommand”构建…”
$vs\u not\u found=“未检测到Visual Studio!”
$vswhere=“${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe”
如果(!(测试路径$vswhere)){throw$vs_not_found}
$vs_ide_path=$(&$vswhere-format value-property productPath)
如果(!(测试路径$vs_ide_Path)){throw$vs_not_found}
$vs_ide_folder=“$(拆分路径$vs_ide_路径)”
$vs_dev_cmd=“$vs_ide_folder/。/Tools/VsDevCmd.bat”
$invoke=“call”“$vs_dev_cmd”“&&cd”“$pwd”“&&buildCommand
powershell -f _invoke-build.ps1 -buildCommand "set INCLUDE | clip"
<#
  .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" }