代码之家  ›  专栏  ›  技术社区  ›  sandimschuh

如何使用Visual Studio 2015(MSBuild 14.0)在中心位置实现解决方案范围的生成配置

  •  0
  • sandimschuh  · 技术社区  · 7 年前

    在Visual Studio 2017和MSBuild 15.0中,引入了一个新功能,通过放置目录来扩展解决方案中所有项目的构建过程。建筑道具和/或目录。建筑目标文件位于解决方案的根文件夹中(即*sln文件所在的文件夹)。

    不幸的是,这个伟大的特性不适用于较旧的VS和MSBuild版本。是否有办法使全局构建配置在VS 2015和MSBuild 14.0中以与VS 2017和MSBuild 15.0类似的方式工作?

    1 回复  |  直到 7 年前
        1
  •  1
  •   sandimschuh    7 年前

    在研究了MSBuild的工作原理后,我发现存在微软。常见的目标位于MSBuildToolsPath目录中的文件(例如C:\Program Files(x86)\MSBuild\14.0\Bin),MSBuild在每次生成期间使用该文件。

    当我比较微软时。常见的目标文件为MSBuild 14.0和MSBuild 15.0,结果表明它们几乎完全相同。主要区别在于,15.0文件包含一些用于发现目录路径的逻辑。建筑目标文件,如果存在这样的文件,则此文件中的生成目标也包含在生成中。

    Differences of MSBuild 14.0 and 15.0 Microsoft.Common.targets files

    *重要注意事项*
    以下解决方案更改了MS Visual Studio安装的部分内容,可能会导致无法预见的严重影响。一般来说,它是危险的,完全没有支撑,可能会破坏各种东西。 请仅在您完全确定自己在做什么的情况下继续。

    对我来说,解决方案只是粘贴包含目录的部分。建筑目标文件来自Microsoft MSBuild 15.0。常见的将文件定向到Microsoft MSBuild 14.0中。常见的目标文件。但是,用15.0文件替换14.0文件也是安全的。 Microsoft的当前版本。常见的可以找到目标文件 here

    为了自动化此过程并在其他开发机器上重复此过程,我制作了此Windows批处理文件,该文件将发现MSBuildTools路径并替换Microsoft。常见的以更新的文件为目标。我在此批处理脚本中使用的更新文件名为MSBuil15。微软常见的目标和必须与批处理文件位于同一文件夹中。此批处理文件基于 MSBuildPath.bat GotAdmin.bat 在这里,我添加了两行代码来替换该文件。

    @echo off
    
    reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath > nul 2>&1
    if ERRORLEVEL 1 goto MissingMSBuildRegistry
    
    for /f "skip=2 tokens=2,*" %%A in ('reg.exe query "HKLM\SOFTWARE\Microsoft\MSBuild\ToolsVersions\14.0" /v MSBuildToolsPath') do SET "MSBUILDDIR=%%B"
    
    echo.
    echo MSBuildToolsPath: %MSBUILDDIR%
    echo.
    
    IF NOT EXIST "%MSBUILDDIR%" goto MissingMSBuildToolsPath
    IF NOT EXIST "%MSBUILDDIR%msbuild.exe" goto MissingMSBuildExe
    IF EXIST "%MSBUILDDIR%Microsoft.Common.targets.bak" goto AlreadyPatched
    
    
    ::-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
        "%temp%\getadmin.vbs"
        exit /B
    
    :gotAdmin
        if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
        pushd "%CD%"
        CD /D "%~dp0"
    ::-------------------------------------- 
    
    
    :: Replace MSBuild 14.0 Microsoft.Common.targets file with MSBuild 15.0 file.
    rename "%MSBUILDDIR%Microsoft.Common.targets" "Microsoft.Common.targets.bak"
    copy "MSBuil15.Microsoft.Common.targets" "%MSBUILDDIR%Microsoft.Common.targets"
    
    
    exit /b 0
    
    goto:eof
    ::ERRORS
    ::---------------------
    :MissingMSBuildRegistry
    echo Cannot obtain path to MSBuild tools from registry
    goto:eof
    :MissingMSBuildToolsPath
    echo The MSBuild tools path from the registry '%MSBUILDDIR%' does not exist
    goto:eof
    :MissingMSBuildExe
    echo The MSBuild executable could not be found at '%MSBUILDDIR%'
    goto:eof
    :AlreadyPatched
    echo The Microsoft.Common.targets file is already patched
    goto:eof