引言
使用 Visual Studio IDE 开发项目时,我们会很自然的 运行/F5 / 右键项目-生成/重新生成/清理
,然后就可以看到对应的结果了.
这些结果通常是:
- 一些可执行文件,
.dll/.exe
等 - 一些弹出的
Console
控制台界面 - 一些网页
- 一些窗体
- ...
但是为什么点击上述的一些操作按钮后,就会出现这样的结果呢?
或者说点击上述的操作按钮后,中间发生了什么?
答案是:MSBuild
MSBuild.exe 路径
通常情况下, 指定版本的 .NET Framework
中内置了 MSBuild.exe
:
C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
- ...
如果你还安装了 Visual Studio 2017
的话, Visual Studio 2017
中也内置了一个 MSBuild.exe
,路径如下:
D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe
Tips:
MSBuild.exe
路径通常对于需要手动/自动执行构建任务的小伙伴有所帮助.
使用实例
Tips: 据说在
Visual Studio
中通过调整工具-选项-项目和解决方案-生成并运行
中的MSBuild 项目生成输出详细信息
列表选项为普通
,执行操作时,可看到对应执行的命令?
最简单用法
通常我们新建/现有的解决方案/项目中会包含.sln/.csproj
文件
# 构建解决方案,使用默认参数MSBuild xxx.sln # 构建项目,使用默认参数MSBuild xxx.csproj
指定环境配置
新建的项目通常包含Debug/Release
这2种默认环境配置(还可以自定义),构建时可以通过以下方式指定
# /property = /p(简写)# 生成 Debug 环境下的内容MSBuild xxx.csproj /p:Configuration=Debug# 生成 Release 环境下的内容MSBuild xxx.csproj /p:Configuration=Release
指定目标操作
在 Visual Studio 中构建时,通常会有 生成/重新生成/清理
等操作,命令如下:
# /target = /t(简写)# 清理MSBuild xxx.csproj /target:Clean# 生成MSBuild xxx.csproj /target:Build# 重新生成MSBuild xxx.csproj /target:Rebuild# 等同于组合操作MSBuild xxx.csproj /target:Clean;Build
指定输出目录
RT.
# MSBuild xxx.csproj /p:OutDic=E:\SomeDir\# 如何指定到项目默认输出目录呢?
参考
time (dotnet msbuild test.csproj -t:Build -p:Configuration=Debug -p:OutDir=bin -p:DebugType=None -p:DebugSymbols=False -restore:False -nologo -detailedSummary -p:nodeReuse=false -p:ResolveProjectReferences=false -consoleLoggerParameters:PerformanceSummary -p:maxCpuCount=1 -p:BuildProjectReferences=false -p:BuildAssemblyReferences=false -p:no-cache=true -p:DependsOnTargets="" && dotnet ./bin/test.dll)
Measure-Command { dotnet msbuild test.csproj -t:Build -p:Configuration=Debug -p:OutDir=bin -p:DebugType=None -p:DebugSymbols=False -restore:False -nologo -detailedSummary -p:nodeReuse=false -p:ResolveProjectReferences=false -consoleLoggerParameters:PerformanceSummary -p:maxCpuCount=16 -p:BuildProjectReferences=false -p:BuildAssemblyReferences=false -p:no-cache=true -p:DependsOnTargets="" | dotnet ./bin/test.dll}
参考文档
- - 不大管用
- - 尝试中
- - 尝试中
- [使用 MSBuild 响应文件 (rsp) 来指定 dotnet build 命令行编译时的大量参数](