Last active
May 9, 2021 17:51
-
-
Save jeremybeavon/5736e0acbf3729092887 to your computer and use it in GitHub Desktop.
Programmatically call MSBuild from C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires the following references: | |
// Microsoft.Build.Engine | |
// Microsoft.Build.Framework | |
using System.Collections.Generic; | |
using Microsoft.Build.BuildEngine; | |
public static class MsBuildHelper | |
{ | |
public static bool RunMsBuild(string projectFile, string target, IDictionary<string, string> properties) | |
{ | |
Engine engine = new Engine(); | |
engine.RegisterLogger(new ConsoleLogger()); | |
BuildPropertyGroup buildProperties = new BuildPropertyGroup(); | |
foreach (KeyValuePair<string, string> property in properties) | |
{ | |
buildProperties.SetProperty(property.Key, property.Value); | |
} | |
return engine.BuildProjectFile(projectFile, new [] { target }, buildProperties); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires the following references: | |
// Microsoft.Build | |
// Microsoft.Build.Engine | |
// Microsoft.Build.Framework | |
using System.Collections.Generic; | |
using Microsoft.Build.BuildEngine; | |
using Microsoft.Build.Evaluation; | |
using Microsoft.Build.Execution; | |
using Microsoft.Build.Framework; | |
public static class MsBuildHelper | |
{ | |
public static BuildResult RunMsBuild(string projectFile, string target, IDictionary<string, string> properties) | |
{ | |
BuildParameters parameters = new BuildParameters(new ProjectCollection()) | |
{ | |
Loggers = new ILogger[] {new ConsoleLogger()} | |
}; | |
return BuildManager.DefaultBuildManager.Build( | |
parameters, | |
new BuildRequestData(projectFile, properties, null, new[] {target}, null)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to get a pre compiled build from this code by adding
property.Add("OutputPath", @"c:\BuildDroplocation");
property.Add("DeployOnBuild", "true");
property.Add("WebPublishMethod", "FileSystem");
property.Add("PrecompileBeforePublish", "true");
However, it placed the build in output location without precompiled, is there any additional properties need to include?