如何从Powershell运行MSBuild而不产生msbuild.exe进程? [英] How to run MSBuild from Powershell without spawning msbuild.exe process?
问题描述
我正在考虑通过直接点击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).
有人这样做吗?运行构建的最简单,最直接的方法是什么?您想指出这两种技术的利弊吗? (我对在与脚本其余部分相同的进程/应用程序域中运行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('fullPath\some.proj')
但是,事实证明,直接将MSBuild嵌入Powershell(V1)中是有问题的:
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 fullPath\some.proj
这篇关于如何从Powershell运行MSBuild而不产生msbuild.exe进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!