MSBuild

引言

使用 Visual Studio IDE 开发项目时,我们会很自然的 运行/F5 / 右键项目-生成/重新生成/清理,然后就可以看到对应的结果了.

这些结果通常是:

  • 一些可执行文件, .dll/.exe
  • 一些弹出的 Console 控制台界面
  • 一些网页
  • 一些窗体
  • ...

但是为什么点击上述的一些操作按钮后,就会出现这样的结果呢?

或者说点击上述的操作按钮后,中间发生了什么?

答案是:MSBuild

MSBuild.exe 路径

通常情况下, 指定版本的 .NET Framework 中内置了 MSBuild.exe :

  • C:WindowsMicrosoft.NETFrameworkv2.0.50727MSBuild.exe
  • C:WindowsMicrosoft.NETFrameworkv3.5MSBuild.exe
  • C:WindowsMicrosoft.NETFrameworkv4.0.30319MSBuild.exe
  • ...

如果你还安装了 Visual Studio 2017 的话, Visual Studio 2017 中也内置了一个 MSBuild.exe ,路径如下:

  • D:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuild15.0BinMSBuild.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

Debug & 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:Res
olveProjectReferences=false  -consoleLoggerParameters:PerformanceSummary -p:maxCpuCount=16 -p:BuildProjectReferences=false -p:BuildAssemblyReferences=false -p:no-cache=true -p:DependsOnTargets=""  | dotnet ./bin/test.dll}

参考文档

原文地址:https://www.cnblogs.com/taadis/p/12125845.html