最近在看一些算法和测试一些程序,以及帮团队测试程序,团队使用了vs开发环境创建的sln项目文件,我使用的是公司的机器,没有任何权限安装程序等操作,但是又需要编译一些程序,所以我想到了,使用MSBuild.exe进行编译。
如果你的机器上没有装有VisualStudio,那么可以使用MSBuild来build .sln或project。可以不用安装vs程序,MSBuild可以通过安装.NETFramework来安装,一般的安装路径为C:WindowsMicrosoft.NETFramework。其实devenv执行build时候,后台也是调用MSBuild来build的。
可以使用msbuild /?来查看详细的帮助;
今天就简单的介绍下使用MSBuild.exe编译程序的方法:
- 确认你使用的vs的版本,这个可以从sln文件中看到,不会看的可以查百度
- 确认你本机已经安装了那个版本的MSBuild.exe程序,具体的可以使用dir查看,如:C:>dir /s /b /d MSBuild.exe 可以查看本机各个版本。
- 使用对应版本的MSBuild.exe编译sln文件,例如编译vs2015编写的程序:
%SystemRoot%Microsoft.NETFrameworkv4.0.30319MSBuild.exe "c: est est1.sln" /property:Configuration=Debug /t:build /p:VisualStudioVersion=14.0
说明:如果你想编译单个文件,或者是测试某段程序算法等,可以不用考虑使用那个版本的MSBuild.exe程序。
简单实例:
同样注意,如果project引用了其他的projects的时候,最好build整个.sln。
=================================================================================================
参考如下:
背景:VS很好很强大,但太费系统资源了,尤其是在虚拟机在里面装VS的时候更是如此。有时用vi编辑了源代码后,不想开VS IDE编译,但每次打开VS命令行,再切换到工程所在目录,然后手动敲命令太麻烦了。于是产生了集成集成VS命令行编译到.sln文件右键菜单的想法。
直接上图:
本版本支持 Win10 + VS2015
出处:http://www.cnblogs.com/crazybird/p/5103906.html
======================================================
临时手写了个,可以参考吧
@ECHO OFF set ms="C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0BinMSBuild.exe" cls %ms% /version color 0c ECHO. ECHO. echo Do you use this version of MSBuild.exe to compile the current program? Close this window if you don't compile. pause color GOTO End IF /I "%1"=="Debug" GOTO BuildDebug IF /I "%1"=="Release" GOTO BuildRelease IF /I "%1"=="All" GOTO BuildAll IF /I "%1"=="Minimal" GOTO BuildMinimal :ER ECHO. ECHO Raspkate Command-Line Build Tool v1.0 ECHO. ECHO Usage: ECHO build.bat Debug ECHO Builds the Raspkate with Debug configuration. ECHO. ECHO build.bat Release ECHO Builds the Raspkate with Release configuration. ECHO. GOTO End :BuildDebug %ms% /p:Configuration="Debug";TargetFrameworkVersion="v4.5.2" Raspkate.sln GOTO End :BuildRelease %ms% /p:Configuration="Release";TargetFrameworkVersion="v4.5.2" Raspkate.sln GOTO End :BuildAll %ms% /p:Configuration="All";TargetFrameworkVersion="v4.5.2" Raspkate.sln GOTO End :BuildMinimal %ms% /p:Configuration="Minimal";TargetFrameworkVersion="v4.5.2" Raspkate.sln GOTO End :End @ECHO ON
优化版本
@ECHO OFF :: 建议使用开发时使用VS的版本对应的MSBuild.exe程序 set ms="C:Program Files (x86)Microsoft Visual Studio2017EnterpriseMSBuild15.0BinMSBuild.exe" cls %ms% /version ECHO, IF /I "%1"=="Debug" GOTO BuildDebug IF /I "%1"=="Release" GOTO BuildRelease IF /I "%1"=="All" GOTO BuildAll IF /I "%1"=="Minimal" GOTO BuildMinimal color 0c ECHO, ECHO, echo Do you use this version of MSBuild.exe to compile the current program? Close this window if you don't compile. echo,&&echo 1:Building projects with Debug models echo,&&echo 2:Building projects with Release models echo,&&echo 3:Building projects with Minimal models echo,&&echo 4:Building projects with All models echo; set /p buildType=Please enter number of your build mode : ::pause color ::GOTO End IF /I "%buildType%"=="1" GOTO BuildDebug IF /I "%buildType%"=="2" GOTO BuildRelease IF /I "%buildType%"=="3" GOTO BuildMinimal IF /I "%buildType%"=="4" GOTO BuildAll :ER ECHO, ECHO MSBuild.exe Command-Line Build Tool v1.0 ECHO, ECHO Usage: ECHO build.bat Debug ECHO Builds the Solution or Project with Debug configuration. ECHO, ECHO build.bat Release ECHO Builds the Solution or Project with Release configuration. ECHO, GOTO End :BuildDebug echo you chose Debug build %ms% /p:Configuration="Debug" Car.sln echo you chose Debug build completed GOTO End :BuildRelease echo you chose Release build %ms% /p:Configuration="Release" Car.sln echo you chose Release build completed GOTO End :BuildAll echo you chose All build %ms% /p:Configuration="All" Car.sln echo you chose All build completed GOTO End :BuildMinimal echo you chose Minimal build %ms% /p:Configuration="Minimal" Car.sln echo you chose Minimal build completed GOTO End :End pause @ECHO ON