Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building programmatically a project

Tags:

c#

msbuild

I need to build a project programmatically for a .csproj I am creating on the fly. While searching Google I found the classes and API provided by the MS for the MSBuild Engine. With that information, I create a process which executes msbuild.exe and then reads the output, but now I want to use the namespace Microsoft.Build.Execution to build the project. This is my program:

public class Compiler
{
   private static string locationOfMSBuilldEXE = "";
   public static void Build(string msbuildFileName)
   {
       BuildManager manager = BuildManager.DefaultBuildManager;

       ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
       var result = manager.Build(new BuildParameters() 
                {
                    DetailedSummary = true
                }, 
                new BuildRequestData(projectInstance, new string[] { "Build" }));
       var buildResult = result.ResultsByTarget["Build"];
       var buildResultItems = buildResult.Items;

       string s = "";
   }
}

The results show that this is building fine, but I need to know the detailed output from the compile and how to view it. It would be really helpful if someone can give me link to a good tutorial or a book on MSBuild.

like image 415
Parv Sharma Avatar asked Mar 30 '12 11:03

Parv Sharma


3 Answers

Thanks @ritchmelton. Though I figured it out myself. Here is my code : I have used an in built logger ConsoleLogger

public class Compiler
    {
        private static string locationOfMSBuilldEXE = "";
        public static void Build(string msbuildFileName)
        {
            ConsoleLogger logger = new ConsoleLogger(LoggerVerbosity.Normal);
            BuildManager manager = BuildManager.DefaultBuildManager;

            ProjectInstance projectInstance = new ProjectInstance(msbuildFileName);
            var result = manager.Build(
                new BuildParameters() 
                {
                    DetailedSummary = true,
                    Loggers = new List<ILogger>(){logger}
                }, 
                new BuildRequestData(projectInstance, new string[] { "Build" }));
            var buildResult = result.ResultsByTarget["Build"];
            var buildResultItems = buildResult.Items;

            string s = "";
        }
    }
like image 152
Parv Sharma Avatar answered Oct 23 '22 02:10

Parv Sharma


You need to add a instance of a class that implements the ILogger interface to your BuildParameters. You can add a new instance of one of the supplied loggers in the Microsft.Build.Logging namespace, or you can implement ILogger yourself as it is very small and there is a helper class in the Microsoft.Build.Utilities namespace called Logger that is easy to extend.

Build loggers

ILogger interface

Logger helper

like image 9
Ritch Melton Avatar answered Oct 23 '22 01:10

Ritch Melton


If you just want to build a project or solution, without elaborate parameters, you can do it more simply. Pseudocode:

using namespace Microsoft.Build.Evaluation;

var p = Project.Load("path to project");
p.SetGlobalProperty("Configuration", "Release");
p.Build(...);

That's it! BuildParameters and so forth are for quite advanced scenarios. Visual Studio itself uses them.

Dan (msbuild dev)

like image 9
cheerless bog Avatar answered Oct 23 '22 01:10

cheerless bog