使用Microsoft.Build.Evaluation.Project

时间:2018-12-27 22:54:42

标签: c# visual-studio msbuild msbuild-task

我正在尝试创建一个控制台应用程序以构建(发布模式)现有项目。

使用“ VS 2017的开发人员命令提示符”,我很想使用以下方法获得预期的结果:

MsBuild.exe "[***].csproj" /t:Build /p:Configuration=Release /p:TargetFramework=v4.5

到目前为止,我有以下代码:

class Program
{
    static void Main(string[] args)
    {
        Run(new FileInfo("[***].csproj"));
        Console.ReadLine();
    }

    public static bool Run(FileInfo msbuildFile, string[] targets = null, IDictionary<string, string> properties = null, LoggerVerbosity loggerVerbosity = LoggerVerbosity.Detailed)
    {
        if (!msbuildFile.Exists) throw new ArgumentException("msbuildFile does not exist");

        if (targets == null)
        {
            targets = new string[] { };
        }
        if (properties == null)
        {
            properties = new Dictionary<string, string>();
        }

        Console.Out.WriteLine("Running {0} targets: {1} properties: {2}, cwd: {3}",
                              msbuildFile.FullName,
                              string.Join(",", targets),
                              string.Join(",", properties),
                              Environment.CurrentDirectory);

        var project = new Project(msbuildFile.FullName, properties, "14.0");

        project.SetGlobalProperty("Configuration", "Release");
        project.SetGlobalProperty("TargetFramework", "v4.5");

        return project.Build(targets, new ILogger[] { new ConsoleLogger(loggerVerbosity) });
    }
}

由于某种原因它失败了。你能告诉我为什么吗? 错误如下:

  

[...] \ BuildScripts \ ModulePackage.Targets(9,5):错误MSB4062:无法从程序集[...] \ BuildScripts中加载“ MSBuild.Community.Tasks.XmlRead”任务。包\ MSBuildTasks.1.5.0.235 \ tools \ MSBuild.Community.Tasks.dll。无法加载文件或程序集“ file:/// [...] \ BuildScripts \ packages \ MSBuildTasks.1.5.0.235 \ tools \ MSBuild.Community.Tasks.dll”或其依赖项之一。该系统找不到指定的文件。确认声明正确无误,并保证程序集及其所有依赖项都可用,并且任务包含实现Microsoft.Build.Framework.ITask的公共类。

0 个答案:

没有答案