如何自动(从命令行)安装 Visual Studio Build Tools 构建环境,适用于 C++、.NET、C# 等
How to automate (from command-line) the installation of a Visual Studio Build Tools build environment, for C++, .NET, C#, etc
注意:我已经阅读了 但这并不能回答完全无 GUI 的仅脚本安装。
多年来,这是我注意到的:
我从Github下载了一个项目;或打开我的一个旧项目(比如 4 年前)
运行 msbuild.exe theproject.sln
糟糕,我没有正确的 Visual Studio 版本/.NET Framework 版本/缺少正确的 SDK(示例情况 here 等等)
然后花 X 小时浏览 https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019 等网站,安装一个包,然后发现它不是正确的(msbuild
仍然失败),下载另一个包,安装它等等
最后你已经下载了 8GB 的包,等待下载,等待安装,等待重启,你仍然不确定它是否有效
您的计算机现在一团糟,同时安装了 5 个不同版本的 SDK,它们可能相互冲突(版本 Z overwrite/uninstall 版本 Y 与否?)
这可能是避免此问题的解决方案:
如何从命令行安装所需的 MS 构建工具?(我不想使用任何 IDE,我想编写所有脚本)
如果可能的话,我会一次性为每个项目创建一个 build.bat
文件,类似于:
msbuildget --package=VC14 --installdir=c:\buildtools\vc14 # automatically download and install
C:\buildtools\vc14\bin\msbuild.exe myproject.sln
或
msbuildget --package=.NET-35 --installdir=c:\buildtools\net35
C:\buildtools\net35\bin\msbuild.exe myproject.sln
怎么做?
用这个方法,即使你打开一个6年的老项目,你应该也能建起来。
How to automate (from command-line) the installation of a Visual
Studio Build Tools build environment, for C++ version X, .NET C#
version Z, etc
首先要注意的是,所有的工作负载或包都需要安装,它们会集成到Build Tool中,你需要的是它们的工作负载组件ID。
您可以参考this document获取buildtool相关的Component ID
除外,this document还列出命令行安装说明。顺序与构建工具相同。
建议
您可以试试下面的脚本:
// This is for desktop development and also add the net framwork 3.5
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools^
--add Microsoft.Net.Component.3.5.DeveloperTools^
--add Microsoft.Net.Component.4.5.2.TargetingPack^
--installPath C:\BuildTools
C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln
并且您可以通过带有相关组件 ID 的命令 -add
添加更多工作负载或包。
如果你想构建c++项目,你可以试试下面的例子:
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.VCTools^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^
--add Microsoft.VisualStudio.Component.VC.140^
--installPath C:\BuildTools
C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln
Microsoft.VisualStudio.Component.VC.140
表示VS2015 C++构建工具。
重要说明:使用命令行与vs_installerUI有很大不同。当你点击vs_installerUI中的c++构建工具时,你会看到它会自动安装相关组件。
这些组件在Microsoft.VisualStudio.Workload.VCTools workload下列出,您可以选择是否安装它们。
但是,并不是指定工作负载全部安装。
当您使用命令行时,它不会自动安装任何相关组件,所以您应该手动添加它们。
对于 c++ 项目,您可以使用这些命令来安装它:
vs_buildtool.exe --add Microsoft.VisualStudio.Workload.MSBuildTools^
--add Microsoft.VisualStudio.Workload.VCTools^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^
--add Microsoft.VisualStudio.Component.Windows10SDK.18362^
--add Microsoft.VisualStudio.Component.VC.CMake.Project^
--add Microsoft.VisualStudio.Component.TestTools.BuildTools^
--add Microsoft.VisualStudio.Component.VC.ASAN^
--add Microsoft.VisualStudio.Component.VC.140
注意:我已经阅读了
多年来,这是我注意到的:
我从Github下载了一个项目;或打开我的一个旧项目(比如 4 年前)
运行
msbuild.exe theproject.sln
糟糕,我没有正确的 Visual Studio 版本/.NET Framework 版本/缺少正确的 SDK(示例情况 here 等等)
然后花 X 小时浏览 https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019 等网站,安装一个包,然后发现它不是正确的(
msbuild
仍然失败),下载另一个包,安装它等等最后你已经下载了 8GB 的包,等待下载,等待安装,等待重启,你仍然不确定它是否有效
您的计算机现在一团糟,同时安装了 5 个不同版本的 SDK,它们可能相互冲突(版本 Z overwrite/uninstall 版本 Y 与否?)
这可能是避免此问题的解决方案:
如何从命令行安装所需的 MS 构建工具?(我不想使用任何 IDE,我想编写所有脚本)
如果可能的话,我会一次性为每个项目创建一个 build.bat
文件,类似于:
msbuildget --package=VC14 --installdir=c:\buildtools\vc14 # automatically download and install
C:\buildtools\vc14\bin\msbuild.exe myproject.sln
或
msbuildget --package=.NET-35 --installdir=c:\buildtools\net35
C:\buildtools\net35\bin\msbuild.exe myproject.sln
怎么做?
用这个方法,即使你打开一个6年的老项目,你应该也能建起来。
How to automate (from command-line) the installation of a Visual Studio Build Tools build environment, for C++ version X, .NET C# version Z, etc
首先要注意的是,所有的工作负载或包都需要安装,它们会集成到Build Tool中,你需要的是它们的工作负载组件ID。
您可以参考this document获取buildtool相关的Component ID
除外,this document还列出命令行安装说明。顺序与构建工具相同。
建议
您可以试试下面的脚本:
// This is for desktop development and also add the net framwork 3.5
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.ManagedDesktopBuildTools^
--add Microsoft.Net.Component.3.5.DeveloperTools^
--add Microsoft.Net.Component.4.5.2.TargetingPack^
--installPath C:\BuildTools
C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln
并且您可以通过带有相关组件 ID 的命令 -add
添加更多工作负载或包。
如果你想构建c++项目,你可以试试下面的例子:
vs_buildtool_xxx.exe --add Microsoft.VisualStudio.Workload.VCTools^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^
--add Microsoft.VisualStudio.Component.VC.140^
--installPath C:\BuildTools
C:\BuildTools\MSBuild\Current\Bin\MSBuild.exe myproject.sln
Microsoft.VisualStudio.Component.VC.140
表示VS2015 C++构建工具。
重要说明:使用命令行与vs_installerUI有很大不同。当你点击vs_installerUI中的c++构建工具时,你会看到它会自动安装相关组件。
这些组件在Microsoft.VisualStudio.Workload.VCTools workload下列出,您可以选择是否安装它们。
但是,并不是指定工作负载全部安装。
当您使用命令行时,它不会自动安装任何相关组件,所以您应该手动添加它们。
对于 c++ 项目,您可以使用这些命令来安装它:
vs_buildtool.exe --add Microsoft.VisualStudio.Workload.MSBuildTools^
--add Microsoft.VisualStudio.Workload.VCTools^
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64^
--add Microsoft.VisualStudio.Component.Windows10SDK.18362^
--add Microsoft.VisualStudio.Component.VC.CMake.Project^
--add Microsoft.VisualStudio.Component.TestTools.BuildTools^
--add Microsoft.VisualStudio.Component.VC.ASAN^
--add Microsoft.VisualStudio.Component.VC.140