如何在不产生 msbuild.exe 进程的情况下从 Powershell 运行 MSBuild?

How to run MSBuild from Powershell without spawning msbuild.exe process?(如何在不产生 msbuild.exe 进程的情况下从 Powershell 运行 MSBuild?)
本文介绍了如何在不产生 msbuild.exe 进程的情况下从 Powershell 运行 MSBuild?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在考虑通过直接点击 MSBuild 程序集从 Powershell 脚本运行 MSBuild(而不是查找 MSBuild 安装路径并将 msbuild.exe 作为子进程启动).

I am considering running MSBuild from a Powershell script by tapping directly to the MSBuild assemblies (as opposed to looking up MSBuild install path and starting msbuild.exe as a child process).

有人做过吗?运行构建的最简单、最直接的方法是什么?您想指出这两种技术是否有任何优点/缺点?(我对在与脚本其余部分相同的进程/appdomain 中运行 msbuild 可能出现的任何问题特别感兴趣).

Has anyone done this? What would be the simplest, most straightforward way to run the build? Are there any pros/cons to either technique you'd like to point out? (I'm especially interested in any issues that might arise from running msbuild in the same process/appdomain as the rest of the script).

目前我的想法是这样的:

Currently my thinking is something along these lines:

[void][System.Reflection.Assembly]::Load('Microsoft.Build.Engine, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][Microsoft.Build.BuildEngine.Engine]::GlobalEngine.BuildProjectFile("path/main.proj")

推荐答案

工作并产生输出的最简单的嵌入式构建调用是:

The simplest embedded-build invocation that worked and produced output was:

[void][System.Reflection.Assembly]::Load('Microsoft.Build.Engine, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
$engine = New-Object Microsoft.Build.BuildEngine.Engine
$engine.RegisterLogger((New-Object Microsoft.Build.BuildEngine.ConsoleLogger))
$engine.BuildProjectFile('fullPathsome.proj')

然而,事实证明直接在 Powershell (V1) 中嵌入 MSBuild 是有问题的:

However, it turns out embedding MSBuild directly in Powershell (V1) is problematic:

'MSBUILD : warning MSB4056: The MSBuild engine must be called on
a single-threaded-apartment. Current threading model is "MTA".
Proceeding, but some tasks may not function correctly.'

为什么我们仍然在 2009 年在托管环境中工作时缴纳 COM 税?

Why oh why are we still paying COM tax in 2009 while working in a managed environment?

我的结论是在 Powershell (V1) 中嵌入 MSBuild 并不是一个好主意.作为参考,我还包括了我最终使用的基于流程的方法:

My conclusion is that embedding MSBuild in Powershell (V1) is not a good idea. For reference, I'm also including the process-based approach I ended up using:

[void][System.Reflection.Assembly]::Load('Microsoft.Build.Utilities.v3.5, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
$msbuild = [Microsoft.Build.Utilities.ToolLocationHelper]::GetPathToDotNetFrameworkFile("msbuild.exe", "VersionLatest")
&$msbuild fullPathsome.proj

这篇关于如何在不产生 msbuild.exe 进程的情况下从 Powershell 运行 MSBuild?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)