c# - 使用 API 时从 MSBUILD 中跳过/排除项目类型

标签 c# msbuild msbuild-api

我正在使用 MSBUILD API 通过服务构建解决方案。

例如

var pc = new ProjectCollection();
var buildProperties = new Dictionary<string, string>
{
   {"Configuration", "Release"},
   {"Platform", "Any CPU"},
   {"OutputPath", _outputPath}
};

var buildParameters = new BuildParameters(pc);

var buildRequest = new BuildRequestData(_buildFile, buildProperties, null, new[] { "Clean", "Rebuild" }, null);            

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

我想要做的是传递一个排除的项目类型或扩展的列表。首先,我想排除:

  • 数据库项目
  • WinRT 项目
  • 通用 MSBUILD 文件(无项目类型 GUID)。

有没有办法通过将一些参数传递给 MSBUILD 管理器来解决这个问题?

最佳答案

这不是完全您想要的,但我只是碰巧在过去的日常工作中弄乱了 msbuild API,并认为这段代码可能有用:

var basePath = "path-to-where-source-is";
var outputDir = "path-to-output";

// Setup some properties that'll apply to all projs
var pc = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
pc.SetGlobalProperty("Configuration", "Debug");
pc.SetGlobalProperty("Platform", "Any CPU");
pc.SetGlobalProperty("OutDir", outputDir);

// Generate the metaproject that represents a given solution file
var slnProjText = SolutionWrapperProject.Generate(
    Path.Combine(basePath, "NAME-OF-SOLUTION-FILE.sln"), 
    "4.0", 
    null);

// It's now a nice (well, ugly) XML blob, so read it in
using(var srdr = new StringReader(slnProjText))
using(var xrdr = XmlReader.Create(srdr))
{
    // Load the meta-project into the project collection        
    var slnProj = pc.LoadProject(xrdr, "4.0");

    // Slice and dice the projects in solution with LINQ to
    // get a nice subset to work with
    var solutionProjects = 
        from buildLevel in Enumerable.Range(0, 10)
        let buildLevelType = "BuildLevel" + buildLevel
        let buildLevelItems = slnProj.GetItems(buildLevelType)
        from buildLevelItem in buildLevelItems
        let include = buildLevelItem.EvaluatedInclude
        where !include.Contains("Some thing I don't want to build")
        select new 
        { 
            Include=include, 
            Project = pc.LoadProject(Path.Combine(basePath, include))
        };

    // For each of them, build em!
    foreach (var projectPair in solutionProjects)
    {
        var project = projectPair.Project;
        var include = projectPair.Include;
        var outputPath = outputDir;
        project.SetProperty("OutputPath", outputPath);
        Console.WriteLine("Building project:" + project.DirectoryPath);
        var buildOk = project.Build("Build");
        if(buildOk)
        {
            Console.WriteLine("Project build success!");
        } 
        else
        {
            throw new Exception("Build failed");
        }
    }
}

关于c# - 使用 API 时从 MSBUILD 中跳过/排除项目类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15124641/

相关文章:

c# - C#[在Unity中]和C++之间的命名管道

.net - 修复了在 Debug模式下测试 Azure 和 Web 服务角色时的端口号

asp.net - 使用 MSBUILD 和/或 MSDEPLOY (.NET 3.5) 从命令行进行部署

msbuild - 当 ItemGroup 文件全部具有相同名称时,如何使用 MSBuild 转换?

c# - 加载包含作为通配符的项的 .NET 项目

c# - sgen.exe 的命令行太长

加载到 RAM 中的 C# 类代码?

c# - 如何将信息从 Kinect 发送到 Arduino?