【问题标题】:Force BuildManager to use another version of MSBuild强制 BuildManager 使用另一个版本的 MSBuild
【发布时间】:2014-08-10 02:40:56
【问题描述】:

以下代码尝试使用BuildManager 以编程方式构建解决方案:

ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "12.0";
pc.Loggers.Add(fileLogger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(solutionName, globalProperty, null, new[] { "Build" }, null);

BuildParameters buildParameters = new BuildParameters(pc)
{
    DefaultToolsVersion = "12.0",
    OnlyLogCriticalEvents = false,
    DetailedSummary = true,
    Loggers = new List<Microsoft.Build.Framework.ILogger> { fileLogger }.AsEnumerable()
};

var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

当我运行这段代码时,它不会构建任何东西。我可以看到除了winmdexp.exe 的一个特定版本之外,还使用了以下编译器csc.exe

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Csc.exe
ExportWindowsMDFile:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\winmdexp.exe 

但是当我使用 VS IDE 成功构建解决方案时,出现以下信息:

C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe
ExportWindowsMDFile:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\winmdexp.exe

为什么我的代码会出现这种情况以及如何更改它?

【问题讨论】:

    标签: c# msbuild solution microsoft.build winmd


    【解决方案1】:

    从 Visual Studio 2013 开始,MSBuild is no longer a .NET Framework component。这意味着对打包和部署进行了一些重组。旧的 MSBuild 框架仍然存在于 .NET Framework 文件夹中。但是,当您安装 Visual Studio 2013 时,Microsoft Build Tools 12.0 也会安装到 C:\Program Files (x86)\MSBuild\12.0。请注意,构建工具与 Visual Studio here 分开提供。

    当我第一次尝试这个时,我也遇到了和你一样的情况。问题是您可能已经引用了旧的“4.0”MSBuild Framework 程序集。您需要引用位于 C:\Program Files (x86)\MSBuild\12.0\bin 中的新 12.0 程序集(您必须在 VS 中浏览以添加引用)。您可能需要 Microsoft.Build、M​​icrosoft.Build.Engine 和 Microsoft.Build.Framework。更新这些参考资料后,我发现它在构建时使用了与 VS 2013 相同的工具。

    【讨论】:

    • 不幸的是,这被埋得很深:You need to reference the new 12.0 assemblies located in C:\Program Files (x86)\MSBuild\12.0\bin (you'll have to browse there in VS to add the references).Gold。谢谢!
    • 同样值得注意的是,我们刚刚升级到 VS2015(但仍针对 .net 4.5.2)并且正在使用最新的 NameOflanguage 功能,因此不得不重新指向 MsBuild\14.0使用 MsBuild 编译的文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    相关资源
    最近更新 更多