Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automate building a solution using MsBuild API 4.0?

I have a bunch of solutions downloaded from the Internet (codeplex etc.) and I want to build them and run a tool over the DLLs. I want to do this via automation.

It has been suggested to use MSBuild API because it will make it easy to get error information and manipulate MsBuild futher to obtain other info. Unfortunately it is hardly documented so:

  1. How do I build an .sln file (via MSBuild API 4.0) ?
  2. How do I capture the error information? (I saw an example on how to output the log to the console, but did not find smth for files)

Thanks!

like image 873
Bogdan Gavril MSFT Avatar asked Mar 13 '12 23:03

Bogdan Gavril MSFT


People also ask

How do you create a solution using MSBuild?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.

Which tasks in MSBuild can be used by developers in code with MSBuild?

MSBuild includes common tasks that you can modify to suit your requirements. Examples are Copy, which copies files, MakeDir, which creates directories, and Csc, which compiles Visual C# source code files.

Is MSBuild build tool?

MSBuild is a build tool that helps automate the process of creating a software product, including compiling the source code, packaging, testing, deployment and creating documentations. With MSBuild, it is possible to build Visual Studio projects and solutions without the Visual Studio IDE installed.


Video Answer


2 Answers

I found a related question on stackoverflow that provides the solution:

running msbuild programmatically

The accepted answer provides good resources:

http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/ec95c513-f972-45ad-b108-5fcfd27f39bc/ Logging Build messages with MSBuild 4.0: http://www.go4answers.com/Example/building-solution-programatically-vs-5395.aspx

Also, there is an example of log usage on msdn: http://msdn.microsoft.com/en-us/library/microsoft.build.framework.ilogger.aspx

like image 94
Bogdan Gavril MSFT Avatar answered Sep 22 '22 14:09

Bogdan Gavril MSFT


Maybe I'm missing something, but why does it have to be the MSBuild API for a simple task like that?

Just from what you've written in the question, I see no need to use the API just to build a solution and capture the output in a text file.
You can use the MSBuild command line tool for this.

Building a solution with MSBuild is as simple as that:

%windir%\Microsoft.net\Framework\v4.0.30319\msbuild.exe MySolution.sln

To capture the output in a text file, you just need to add this:
(example copied from the link)

/l:FileLogger,Microsoft.Build;logfile=MyLog.log

So the final statement looks like this:

%windir%\Microsoft.net\Framework\v4.0.30319\msbuild.exe MySolution.sln /l:FileLogger,Microsoft.Build;logfile=MyLog.log

This will build the solution and save the output of MSBuild in a text file named MyLog.log in the current directory.

like image 45
Christian Specht Avatar answered Sep 22 '22 14:09

Christian Specht