如何使用 C# 以编程方式构建解决方案?

发布于 2024-11-17 17:11:06 字数 158 浏览 2 评论 0原文

如何以编程方式构建 C# 解决方案?

我应该能够传递解决方案的路径并获取输出消息(或者只是构建解决方案)。我如何在 C# 中实现这一目标?

我需要这个,因为我们正在为我们的项目构建一个单一的解决方案,而它现在从 SVN 获取所有内容并构建它。接下来将是一键部署所有内容。

How do I build a C# solution programmatically?

I should be able to pass the path of a solution and get the output messages (or just build the solution). How do I achieve this in C#?

I need this because we are building a single solution for our projects when it now gets everything from SVN and also builds it. Next would be deploying it all in one click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

孤独患者 2024-11-24 17:11:06

请参阅.NET 4.0 MSBuild API 简介 使用 .NET 4.0 MSBuild API 的示例:

List<ILogger> loggers = new List<ILogger>();
loggers.Add(new ConsoleLogger());
var projectCollection = new ProjectCollection();
projectCollection.RegisterLoggers(loggers);
var project = projectCollection.LoadProject(buildFileUri); // Needs a reference to System.Xml
try
{
    project.Build();
}
finally
{
    projectCollection.UnregisterAllLoggers();
}

一个更简单的示例:

var project = new Project(buildFileUri, null, "4.0");
var ok = project.Build(); // Or project.Build(targets, loggers)
return ok;

记住使用.NET 4 配置文件(而不是客户端配置文件)。

添加以下引用:System.XML、Microsoft.Build、M​​icrosoft.Build.Framework 以及可选的 Microsoft.Build.Utilities.v4.0。

另请参阅 Stack Overflow 问题以编程方式运行 MSBuild

要构建解决方案,请执行以下操作:

var props = new Dictionary<string, string>();
props["Configuration"] = "Release";
var request = new BuildRequestData(buildFileUri, props, null, new string[] { "Build" }, null);
var parms = new BuildParameters();
// parms.Loggers = ...;

var result = BuildManager.DefaultBuildManager.Build(parms, request);
return result.OverallResult == BuildResultCode.Success;

See .NET 4.0 MSBuild API introduction for an example using the .NET 4.0 MSBuild API:

List<ILogger> loggers = new List<ILogger>();
loggers.Add(new ConsoleLogger());
var projectCollection = new ProjectCollection();
projectCollection.RegisterLoggers(loggers);
var project = projectCollection.LoadProject(buildFileUri); // Needs a reference to System.Xml
try
{
    project.Build();
}
finally
{
    projectCollection.UnregisterAllLoggers();
}

A simpler example:

var project = new Project(buildFileUri, null, "4.0");
var ok = project.Build(); // Or project.Build(targets, loggers)
return ok;

Remember to use the .NET 4 Profile (not the Client profile).

Add the following references: System.XML, Microsoft.Build, Microsoft.Build.Framework, and optionally Microsoft.Build.Utilities.v4.0.

Also look at Stack Overflow question Running MSBuild programmatically.

To build a solution, do the following:

var props = new Dictionary<string, string>();
props["Configuration"] = "Release";
var request = new BuildRequestData(buildFileUri, props, null, new string[] { "Build" }, null);
var parms = new BuildParameters();
// parms.Loggers = ...;

var result = BuildManager.DefaultBuildManager.Build(parms, request);
return result.OverallResult == BuildResultCode.Success;
微凉徒眸意 2024-11-24 17:11:06

大多数答案都提供了通过调用外部命令来完成此操作的方法,但是有一个 API,Microsoft.Build.Framework,通过 C# 构建。


博客文章中的代码:

using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class SolutionBuilder
{
    BasicFileLogger b;
    public SolutionBuilder() { }

    [STAThread]
    public string Compile(string solution_name,string logfile)
    {
        b = new BasicFileLogger();
        b.Parameters = logfile;
        b.register();
        Microsoft.Build.BuildEngine.Engine.GlobalEngine.BuildEnabled = true;
        Project p = new Project (Microsoft.Build.BuildEngine.Engine.GlobalEngine);
        p.BuildEnabled = true;
        p.Load(solution_name);
        p.Build();
        string output = b.getLogoutput();
        output += “nt” + b.Warningcount + ” Warnings. “;
        output += “nt” + b.Errorcount + ” Errors. “;
        b.Shutdown();
        return output;
    }
}
// The above class is used and compilation is initiated by the following code,
static void Main(string[] args)
{
    SolutionBuilder builder = new SolutionBuilder();
    string output = builder.Compile(@”G:CodesTestingTesting2web1.sln”, @”G:CodesTestingTesting2build_log.txt”);
    Console.WriteLine(output);
    Console.ReadKey();
}

请注意该博客文章中的代码可以工作,但有点过时。

Microsoft.Build.BuildEngine

已分解为几个部分。

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

Most of the answers are providing ways to do it by calling external commands, but there is an API, Microsoft.Build.Framework, to build via C#.


Code from blog post:

using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

public class SolutionBuilder
{
    BasicFileLogger b;
    public SolutionBuilder() { }

    [STAThread]
    public string Compile(string solution_name,string logfile)
    {
        b = new BasicFileLogger();
        b.Parameters = logfile;
        b.register();
        Microsoft.Build.BuildEngine.Engine.GlobalEngine.BuildEnabled = true;
        Project p = new Project (Microsoft.Build.BuildEngine.Engine.GlobalEngine);
        p.BuildEnabled = true;
        p.Load(solution_name);
        p.Build();
        string output = b.getLogoutput();
        output += “nt” + b.Warningcount + ” Warnings. “;
        output += “nt” + b.Errorcount + ” Errors. “;
        b.Shutdown();
        return output;
    }
}
// The above class is used and compilation is initiated by the following code,
static void Main(string[] args)
{
    SolutionBuilder builder = new SolutionBuilder();
    string output = builder.Compile(@”G:CodesTestingTesting2web1.sln”, @”G:CodesTestingTesting2build_log.txt”);
    Console.WriteLine(output);
    Console.ReadKey();
}

Note the code in that blog post works, but it is a little dated. The

Microsoft.Build.BuildEngine

has been broken up into some pieces.

Microsoft.Build.Construction

Microsoft.Build.Evaluation

Microsoft.Build.Execution

翻身的咸鱼 2024-11-24 17:11:06
// Fix to the path of your MSBuild executable
var pathToMsBuild = "C:\\Windows\\DotNet\\Framework\\msbuild.exe";

Process.Start(pathToMsBuild + " " + pathToSolution);
// Fix to the path of your MSBuild executable
var pathToMsBuild = "C:\\Windows\\DotNet\\Framework\\msbuild.exe";

Process.Start(pathToMsBuild + " " + pathToSolution);
成熟的代价 2024-11-24 17:11:06

您可以创建一个 .proj 文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <!-- Common -->
    <Solution Include="Common\Util\Util.sln"/>
    <Solution Include="Common\EventScheduler\EventSchedulerSolution\EventSchedulerSolution.sln"/>
    <!-- Server -->
    <Solution Include="Server\DataLayer\DataTransferObjects\SharedModel\SharedModel.sln"/>
    <Solution Include="Server\DataLayer\DataTier\ESPDAL.sln"/>
    <!-- Internal Tools -->
    <Solution Include="InternalTools\ServerSchemaUtility\ServerSchemaUtility.sln"/>
  </ItemGroup>
  <Target Name="Rebuild">
    <MSBuild Projects="@(Solution)" Targets="Rebuild" Properties="Configuration=Release"/>
  </Target>
</Project>

然后使用项目文件作为参数调用 msbuild.exe。下面是一个批处理文件示例。在 C# 中,您可以调用 Process.Start,如其他海报所示。

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" BuildSolutions.proj

pause

You can create a .proj file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <!-- Common -->
    <Solution Include="Common\Util\Util.sln"/>
    <Solution Include="Common\EventScheduler\EventSchedulerSolution\EventSchedulerSolution.sln"/>
    <!-- Server -->
    <Solution Include="Server\DataLayer\DataTransferObjects\SharedModel\SharedModel.sln"/>
    <Solution Include="Server\DataLayer\DataTier\ESPDAL.sln"/>
    <!-- Internal Tools -->
    <Solution Include="InternalTools\ServerSchemaUtility\ServerSchemaUtility.sln"/>
  </ItemGroup>
  <Target Name="Rebuild">
    <MSBuild Projects="@(Solution)" Targets="Rebuild" Properties="Configuration=Release"/>
  </Target>
</Project>

And then call msbuild.exe using the project file as an argument. Below is a batch file example. From C#, you could call Process.Start as indicated by other posters.

"C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" BuildSolutions.proj

pause
划一舟意中人 2024-11-24 17:11:06

如果您需要从 Visual Studio 扩展代码触发构建,则必须考虑 IVsBuildManagerAccessor 接口施加的限制 - 请参阅
一般说明,包括新的 IVsBuildManagerAccessor.docx
来自项目的托管包框架
它的分支也可在在 GitHub< /a>.

在带有 MSBuild 4.0 的 Visual Studio 2010 中,有新的交互
影响项目的解决方案构建管理器和 MSBuild 之间的关系
使用这些服务的系统。 MSBuild 4.0 包含一个新的
称为构建管理器的组件(不要与
解决方案构建管理器(VS 组件)控制
提交构建请求。随着 Visual Studio 的出现,这变得很有必要
2010 现在允许并行构建(特别是本机项目)并且
对共享资源(例如 CPU)的访问需要进行调解。为了
以前简单调用 Project.Build() 的项目系统
调用构建必须进行一些更改。现在的项目系统
必须:

  1. 使用 IServiceProvider 接口请求 SVsBuildManagerAccessor 服务。这应该在
    项目系统已在任何构建发生之前加载。
  2. 如果需要 UI 线程,则通知系统
  3. 如果您正在进行设计时构建,请通知系统。
  4. 使用构建管理器访问器注册其记录器。
  5. 将构建请求直接提交给 MSBuild 构建管理器,而不是调用项目上的方法。

If you need to trigger the build from Visual Studio extension code, you must consider the limitations imposed by IVsBuildManagerAccessor interface - see the
General Notes, including new IVsBuildManagerAccessor.docx
from Managed Package Framework for Projects.
Its fork is also available at GitHub.

In Visual Studio 2010 with MSBuild 4.0, there are new interactions
between the Solution Build Manager and MSBuild which impact project
systems utilizing these services. MSBuild 4.0 contains a new
component called the Build Manager (not to be confused with the
Solution Build Manager which is a VS component) which controls the
submission of build requests. This became necessary as Visual Studio
2010 now allows parallel builds to occur (notably native projects) and
access to shared resources such as the CPU needed to be mediated. For
project systems which previously simply called Project.Build() to
invoke a build several changes must be made. The project system now
must:

  1. Ask for the SVsBuildManagerAccessor service using the IServiceProvider interface. This should be done soon after the
    project system is loaded, well before any builds might occur.
  2. Notify the system if you need the UI thread
  3. Notify the system if you are doing a design-time build.
  4. Register its loggers using the Build Manager Accessor.
  5. Submit build requests directly to the MSBuild Build Manager, rather than invoking a method on the Project.
血之狂魔 2024-11-24 17:11:06

当然,您可以使用 MSBuild 构建任何 Visual Studio 解决方案文件。

我相信您可以使用 Process.Start 使用适当的参数调用 MSBuild。

Surely you can use MSBuild to build any Visual Studio solution file.

I believe you can use Process.Start to invoke MSBuild with appropriate parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文