仅使用MSBuild BuildManager构建单个项目

时间:2018-11-19 12:17:05

标签: c# visual-studio msbuild

我有以下代码,它们假定在我的解决方案中编译一个项目。编译成功,但是我的问题是解决方案中的其他项目也已编译,我想避免这种情况。

public static void msBuild(Project p) {
    var pc = new Microsoft.Build.Evaluation.ProjectCollection();
    Dictionary<string, string> GlobalProperty = new Dictionary<string, string>() {
        {"Configuration", "Debug" },
        {"Platform", "AnyCPU" },
    };
    //TODO: MSBuild is compiling some other projects besides the one specified.
    BuildRequestData BuidlRequest = new BuildRequestData(
        p.paths.project, //this is path to project's .csproj file
        GlobalProperty,
        null,
        new string[] { "Build" },
        null
    );
    var bp = new BuildParameters(pc);
    bp.Loggers = new ILogger[] {
        new MSBuildLogger(){
            Verbosity = LoggerVerbosity.Detailed,
        }
    };
    bp.DetailedSummary = true;
    log($"********Compiling {p} project with MSBuild***********");
    BuildManager.DefaultBuildManager.ResetCaches();
    var br = BuildManager.DefaultBuildManager.Build(
        bp, BuidlRequest);
    logBuffer();
    log("Compilation result:", br.OverallResult);
    if (br.Exception != null) throw br.Exception;
}

这是一些来自互联网的基本示例。我不能说我理解这应该如何工作。我已经尝试读取日志,但是甚至不难区分何时要编译新项目。 我知道这会从+开始编译其他项目,当我在其他项目中产生一些错误时就可以看到它。

我希望获得与在 Solution Explorer 中的项目上单击 Build 时相同的行为(我在Configuration Manager中未选中所有项目的构建)

0 个答案:

没有答案