使用微软的MSBuild.exe编译VS .sln .csproj 文件

msbuild官方参考文档

MSBuild 命令行参考

=======================================================================================

如何用msbuid编译项目

msbuid编译我们用vs写好代码以后,用vs编译一下就生成相应的bin文件 ,但有时项目比较大,每次都要重新打开vs加载很浪费时间 ,

我们这里采用直接调用vs自带的msbuild的方式来编译。这样就不用每次都开启vs了。

用vs自带的命令调用msbuild

用msbuild编译一种方式时用vs 自带命令打开。 在vs的命令提示下,输入

msbuild ***.sln  /p:Configuration=Debug 

默认采用的就是debug build ,所以 /p:Configuration=Debug 可以不加

如果想编译成Release

改成

msbuild ***.sln  /p:Configuration=Release

 

 

添加环境变量直接cmd编译

在计算机属性-》属性-》高级系统设置 =》环境变量=》找到path=》在后面添加msbuid路径

;C:\Program Files (x86)\MSBuild\12.0\Bin

我这里采用的是vs2013中的msbuid

vs2017msbuild所在位置 是

;C:\Program Files (x86)\MSBuild\15.0\Bin

vs2015是14,msbuild版本不一样。

;C:\Program Files (x86)\MSBuild\14.0\Bin

添加完环境变量以后,就可以直接在cmd下编译了。 到项目文件夹下面,按shift+右键 , 从此处打开命令窗口。 输入

msbuild consold1.sln 

 

 

之后到bin目录 下查看,发现已经有debug文件夹生成

 

 

相应的,如果采用/p:Configuration=Release 会有相应的release文件生成。

直接用批处理来编译,不用每次输入了

把以下文件写入bat文件中,以后直接执行就可以了。

msbuild console1.sln /p:Configuration=Release

如果要指定输出目录的话,加上outputpath ,中间用;号进行连

msbuild /p:Configuraton=release;outputpath=e:\temp12333

 

 

如果编译指定的项目 可以用如下命令

msbuild c2/c2.csproj /p:Configuration=Release;outputpath=E:\project\demo\console1\c2Release

 

出处:http://www.hechunbo.com/index.php/archives/199.html

=======================================================================================

命令行调用MSBuild编译项目时指定参数

为了方便打包测试,自己PowerShell写了一个编译和发布的脚本,调用msbuild通过命令行来编译当前解决方案

后来发现一个问题,用VS编译解决方案,我通过 项目属性-Build设置 Release和x86

但是脚本直接调用MSBuild编译,有一个项目的属性死活就是Release和Any Cpu

后来看MSBuild的参数,有一个是可以指定的这两个属性的,就是 /property 缩写 /p

 

MSBuild添加下面的参数

/p:Configuration=Release;Platform=x86

 

后续:今天又遇到了VS可以正常编译,但是用msbuild不行的问题

后来发现可以指定target来对应编译方式

/target:Clean;Rebuild

 

出处:https://www.cnblogs.com/wormday/p/7020467.html

=======================================================================================

msbuild命令编译项目文件的参数

最近研究使用发布机发布项目,固然需要使用命令行来编译项目文件,此次的编译使用的是msbuild,也是当下官方推荐;当然还有devenv;
另外说明:本次的实验使用的为Visual Studio 2017
##相关参考

##指令目录说明
在vs2017之前,msbuild都是跟随.net framework一起发布,在vs2017后需要兼容.net core的编译,所以与vs打包整合了,如果用的是visual studio 2017或者使用了c# 5.0中的新特性,建议使用msbuild V15.0的版本来编译;V15.0的目录也是在visual studio的安装目录下有单独的msbuild目录。
##指令语法
###指令的使用语法
MSBuild.exe [选项] [项目文件 | 目录]
###常用开关

开关开关缩写描述示例
/target: /t: 在此项目中生成这些目标。使用分号或逗号分隔多个目标,或者分别指定每个目标。 /target:Resources;Compile
/property:< n >=< v > /p: 设置或重写这些项目级属性。< n > 是属性名,< v > 为属性值。请使用分号或逗号分隔多个属性,或者分别指定每个属性。 /property:WarningLevel=2;OutDir=bin\Debug\
/toolsversion:< version > /tv 要在生成过程中使用的 MSBuild 工具集(任务、目标等)的版本。此版本将重写个别项目指定的版本 /toolsversion:3.5

的以上常用的指令中用的最多的为前两个 /target:和/property:,target用来指定生成项目的目标等;而/property则是一相相当重要的开关属性了下面来特别说明它是如何重要的。
###开关讲解

/Property:< n >= < v >

首先来看我们的项目文件.csproj文件中的一个片段

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
  </Target>

示例中我们可以看到有请多的宏定义$(MSBuildBinPath)、$(VSToolsPath)等,而这些宏是可以使用/property来进行定义的;也就是说.csproj文件中的宏都可以使用/property来进行定义;下面来看一个例子:
命令如下:

msbuild d:\Projects\Web\Web.csproj /t:ResolveReferences;Compile /t:_WPPCopyWebApplication /p:Configuration=Release /p:WebProjectOutputDir=d:\website
在项目中使用了生成事件,copy了一个文件;使用了$(SolutionDir)的宏指令
执行后的错误提示:
错误提示
解决办法:
添加 /p 开关如下 :/p:SolutionDir=‘指定的路经’ 这样问题就解决了

扩展问题:如果自定义一些宏了,可以使用/property来定义吗?
##结语
好了,先写这么多,很久没有写blog了,感觉写出来有头没尾,草草的结个尾

 

出处:https://blog.csdn.net/bklydxz/article/details/77933222

=======================================================================================

=======================================================================================

最近在看一些算法和测试一些程序,以及帮团队测试程序,团队使用了vs开发环境创建的sln项目文件,我使用的是公司的机器,没有任何权限安装程序等操作,但是又需要编译一些程序,所以我想到了,使用MSBuild.exe进行编译。

如果你的机器上没有装有VisualStudio,那么可以使用MSBuild来build .sln或project。可以不用安装vs程序,MSBuild可以通过安装.NETFramework来安装,一般的安装路径为C:\Windows\Microsoft.NET\Framework。其实devenv执行build时候,后台也是调用MSBuild来build的。

可以使用msbuild /?来查看详细的帮助;

今天就简单的介绍下使用MSBuild.exe编译程序的方法:

  1. 确认你使用的vs的版本,这个可以从sln文件中看到,不会看的可以查百度
  2. 确认你本机已经安装了那个版本的MSBuild.exe程序,具体的可以使用dir查看,如:C:\>dir /s /b /d  MSBuild.exe 可以查看本机各个版本。
  3. 使用对应版本的MSBuild.exe编译sln文件,例如编译vs2015编写的程序:
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe "c:\test\test1.sln" /property:Configuration=Debug /t:build /p:VisualStudioVersion=14.0

说明:如果你想编译单个文件,或者是测试某段程序算法等,可以不用考虑使用那个版本的MSBuild.exe程序。

 

简单实例: 

MSBuild MyApp.sln /t:Rebuild /p:Configuration=Release
MSBuild MyApp.csproj /t:Clean /p:Configuration=Debug;/p:Platform=x86;TargetFrameworkVersion=v3.5

同样注意,如果project引用了其他的projects的时候,最好build整个.sln。 

 

参考: MSBuild入门     MSBuild入门(续)     MSBuild功能介绍  

=======================================================================================

参考如下:

背景:VS很好很强大,但太费系统资源了,尤其是在虚拟机在里面装VS的时候更是如此。有时用vi编辑了源代码后,不想开VS IDE编译,但每次打开VS命令行,再切换到工程所在目录,然后手动敲命令太麻烦了。于是产生了集成集成VS命令行编译到.sln文件右键菜单的想法。 

 

直接上图:

 

 

 

本版本支持 Win10 + VS2015 

 

*******************************************************************
*
*  小工具:不用打开项目,右键直接编译*.sln,*.csproj,绿色环保
*
*******************************************************************
 
需要三步:
1 必需copy本目录到C:\
2 运行Install.reg。(反安装运行Unstall.reg)
3 右键*.sln 或 *.csproj 文件会有相应编译菜单出现。

下载: VSBuild(Win10-VS2015).zip

  

出处:http://www.cnblogs.com/crazybird/p/5103906.html

=======================================================================================

个人使用

临时手写了个,可以参考吧,把以下内容保存到MSBuildSln.bat

@ECHO OFF
set ms="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.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
View Code

版本一

优化:增加提示编译模式的显示,可以直接运行MSBuildSln.bat或者MSBuildSln.bat debug的模式运行,以及其他界面提醒等

@ECHO OFF
:: 建议使用开发时使用VS的版本对应的MSBuild.exe程序
set ms="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.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
View Code

版本二

优化:改用参数的方式合并多个编译模式,使用说明,界面提醒等

@ECHO OFF
CLS
ECHO,&ECHO MSBuild.exe Command-Line Build Tool v1.2
:: 建议使用开发时使用VS的版本对应的MSBuild.exe程序
set ms="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
echo;&echo ------------------------------[ MSBuild.exe version info ]------------------------------
%ms% /version
echo; &echo ---------------------------------------------------------------------------------------
ECHO;
SET buildType=%~1
IF /I "%buildType%"=="Debug" GOTO BuildSln
IF /I "%buildType%"=="Release" GOTO BuildSln
IF /I "%buildType%"=="All" GOTO BuildSln
IF /I "%buildType%"=="Minimal" GOTO BuildSln


echo,&color 0A
echo Do you use this version of MSBuild.exe to compile the current program? 
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,&&echo Close this window if you don't compile.
set choseNum=0&echo;&set /p choseNum=  Please enter number of your build mode :
color

IF /I "%choseNum%"=="1" set buildType=Debug&goto BuildSln
IF /I "%choseNum%"=="2" set buildType=Release&goto BuildSln
IF /I "%choseNum%"=="3" set buildType=All&goto BuildSln
IF /I "%choseNum%"=="4" set buildType=Minimal&goto BuildSln


:ER
CLS&ECHO,&COLOR 0C&ECHO ------------------------------[ Help Info ]------------------------------
ECHO Usage:
ECHO     use [ %0 Debug ]
ECHO         Builds the Solution or Project with Debug configuration.
ECHO,
ECHO     use [ %0 Release ]
ECHO         Builds the Solution or Project with Release configuration.
ECHO,
ECHO     use [ %0 Minimal ]
ECHO         Builds the Solution or Project with Minimal configuration.
ECHO,
ECHO     use [ %0 All ]
ECHO         Builds the Solution or Project with All configuration.
ECHO,
GOTO End


:BuildSln
echo; &echo BuildSln : you chose [ %buildType% ] build
ping 127.1>nul 
%ms% /p:Configuration="%buildType%";Platform=x86 /target:Clean;Rebuild Car.sln
echo; &echo BuildSln : you [ %buildType% ] build completed
GOTO :End


:End
echo;&pause&color
goto :eof
View Code

版本三

优化:1)修复一些bug,2)使用自动颜色。3)使用倒计时

@ECHO OFF
CLS
ECHO,&ECHO MSBuild.exe Command-Line Build Tool v1.2
:: 建议使用开发时使用VS的版本对应的MSBuild.exe程序
set ms="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
echo;&echo ---------------------[ MSBuild.exe version info ]---------------------
%ms% /version
echo;&echo ----------------------------------------------------------------------
ECHO;
SET buildType=%~1
IF /I "%buildType%"=="Debug" GOTO BuildSln
IF /I "%buildType%"=="Release" GOTO BuildSln
IF /I "%buildType%"=="All" GOTO BuildSln
IF /I "%buildType%"=="Minimal" GOTO BuildSln


echo,&color 0A
echo Do you use this version of MSBuild.exe to compile the current program? 
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,&&echo Close this window if you don't compile.
set choseNum=0&echo;&set /p choseNum=  Please enter number of your build mode :

IF /I "%choseNum%"=="1" set buildType=Debug&goto BuildSln
IF /I "%choseNum%"=="2" set buildType=Release&goto BuildSln
IF /I "%choseNum%"=="3" set buildType=All&goto BuildSln
IF /I "%choseNum%"=="4" set buildType=Minimal&goto BuildSln



:ER
CLS&ECHO,&COLOR 0C&ECHO ---------------------------[ Help Info ]---------------------------
ECHO Usage:
ECHO     use [ %0 Debug ]
ECHO         Builds the Solution or Project with Debug configuration.
ECHO,
ECHO     use [ %0 Release ]
ECHO         Builds the Solution or Project with Release configuration.
ECHO,
ECHO     use [ %0 Minimal ]
ECHO         Builds the Solution or Project with Minimal configuration.
ECHO,
ECHO     use [ %0 All ]
ECHO         Builds the Solution or Project with All configuration.
ECHO,
GOTO End


:BuildSln
echo;&echo ----------------------------------------------------------------------
echo;&color&echo BuildSln : you chose [ %buildType% ] build
set args=-property:Configuration=%buildType%;Platform=x86 -target:Clean;Rebuild -restore -maxcpucount
echo;&echo BuildSln parameter : %args%
timeout /nobreak /t 15
::%ms% -property:Configuration="%buildType%";Platform=x86 -target:Clean;Rebuild Cares.SMIS.sln -restore -maxcpucount 
%ms% Cares.SMIS.sln %args%
echo; &echo BuildSln : you [ %buildType% ] build completed
GOTO :End


:End
echo;&pause
goto :eof
View Code

 版本四

优化:1)使用配置项指定解决方案路径,可以把脚本保存在任何路径下执行

@ECHO OFF
CLS
ECHO,&ECHO MSBuild.exe Command-Line Build Tool v1.3
:: 建议使用开发时使用VS的版本对应的MSBuild.exe程序
set ms="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\MSBuild.exe"
set slnPath="D:\_Del\Car.xxxx.sln"
echo;&echo ---------------------[ MSBuild.exe version info ]---------------------
%ms% /version
echo;&echo ----------------------------------------------------------------------
echo 即将编译解决方案或项目路径:
echo;&echo 【%slnPath%】
echo;&echo ----------------------------------------------------------------------

SET buildType=%~1
IF /I "%buildType%"=="Debug" GOTO BuildSln
IF /I "%buildType%"=="Release" GOTO BuildSln
IF /I "%buildType%"=="All" GOTO BuildSln
IF /I "%buildType%"=="Minimal" GOTO BuildSln


echo,&color 0A
echo Do you use this version of MSBuild.exe to compile the current program? 
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,&&echo Close this window if you don't compile.
set choseNum=0&echo;&set /p choseNum=  Please enter number of your build mode :

IF /I "%choseNum%"=="1" set buildType=Debug&goto BuildSln
IF /I "%choseNum%"=="2" set buildType=Release&goto BuildSln
IF /I "%choseNum%"=="3" set buildType=All&goto BuildSln
IF /I "%choseNum%"=="4" set buildType=Minimal&goto BuildSln



:ER
CLS&ECHO,&COLOR 0C&ECHO ---------------------------[ Help Info ]---------------------------
ECHO Usage:
ECHO     use [ %0 Debug ]
ECHO         Builds the Solution or Project with Debug configuration.
ECHO,
ECHO     use [ %0 Release ]
ECHO         Builds the Solution or Project with Release configuration.
ECHO,
ECHO     use [ %0 Minimal ]
ECHO         Builds the Solution or Project with Minimal configuration.
ECHO,
ECHO     use [ %0 All ]
ECHO         Builds the Solution or Project with All configuration.
ECHO,
GOTO End


:BuildSln
echo;&echo ----------------------------------------------------------------------
echo;&color&echo BuildSln : you chose [ %buildType% ] build
set args=-property:Configuration=%buildType%;Platform=x86 -target:Clean;Rebuild -restore -maxcpucount
echo;&echo BuildSln parameter : %args%
echo;&echo Compiling solution or project path:【%slnPath%】
timeout /nobreak /t 15
::%ms% -property:Configuration="%buildType%";Platform=x86 -target:Clean;Rebuild Car.xxxx.sln -restore -maxcpucount 
%ms% %slnPath% %args%
echo; &echo BuildSln : you [ %buildType% ] build completed
GOTO :End


:End
echo;&pause
goto :eof
View Code

 

posted on 2017-07-31 16:27  jack_Meng  阅读(17001)  评论(0编辑  收藏  举报

导航