以编程方式从解决方案构建中排除项目

时间:2018-02-21 12:46:04

标签: c# msbuild

我使用BuildManager类创建了一个使用解决方案构建项目的应用程序。 我试图在我的解决方案中排除测试项目。 C#代码中是否有一种方法可以排除与特定名称匹配的项目?例如*.test.proj

到目前为止,这是我的代码:

public bool Build()
{
    Dictionary<string, string> globalProperty = new Dictionary<string, string>
    {
        { "Configuration", "Debug" },
        { "Platform", "Any CPU" },
        { "VisualStudioVersion", "14.0"}
    };

    string pathToLog = ConfigHelper.CheckConfig("ServiceBuildLogPath");

    ProjectCollection pc = new ProjectCollection();
    BuildParameters bp = new BuildParameters(pc)
    {
        Loggers = new List<Microsoft.Build.Framework.ILogger>() { new Microsoft.Build.BuildEngine.FileLogger() { Parameters = "logfile=" + pathToLog + @"MSBuild.log" } },
        DefaultToolsVersion = "14.0"
    };

    BuildResult buildResult = null;

    BuildRequestData buildRequest;

    foreach (Solution solution in _services)
    {
        try
        {
            if (File.Exists(solution.PathToSolution))
            {
                if (_nugetPackageRestore.Restore(solution.PathToSolution))
                {
                    buildRequest = new BuildRequestData(solution.PathToSolution, globalProperty, "4.0", new string[] { "Build" }, null);
                    buildResult = Microsoft.Build.Execution.BuildManager.DefaultBuildManager.Build(bp, buildRequest);

                    if (buildResult.OverallResult != BuildResultCode.Success)
                    {
                        if (buildResult.Exception != null)
                        {
                            throw buildResult.Exception;
                        }

                        throw new Exception(string.Format("Unable to build solution {0}", solution.Name));
                    }
                }
            }
            else
            {
                throw new FileNotFoundException(string.Format("Solution {0} is unavailable", solution.Name));
            }
        }
        catch
        {
            throw;
        }
    }

    return buildResult != null && buildResult.OverallResult == BuildResultCode.Success;
}

1 个答案:

答案 0 :(得分:0)

以下是仅在构建中包含特定项目/解决方案文件的替代方法。

Download only required solutions from workspace