25 March 2009

Book Review: Inside the Microsoft Build Engine: Using MSBuild and Team Foundation Build

Quote for the Mediacover ;-)

“Wow!  This is the book that I wish I had written.  Sayed and William have covered the topic’s depth and breadth with enough illustrations and code examples to make any developer productive with MSBuild and Team Build.  No Experience Required. “

Disclaimer

First, let me start by saying that I have no affiliation to the authors or Microsoft Press.  I purchased my copy of the book from Amazon.com and have been in no way influenced in my opinion other than by the book itself.

I’m writing this review because I feel that this book is an essential tome if you are serious about using MSBuild and/or Team Build to automate your build process and are serious about delivering quality applications to your users/customers.  I have been on the fence about writing a book on this subject for a couple of years now but have been daunted by the time and energy it takes to “do it right”.  Well now I am relieved to say that I no longer have to.  This book covers the topic more thoroughly that I could have. 

Review

The book is broken down into 2 sections, MSBuild first then Team Build later. 

MSBuild

The MSBuild section starts off with a “Quick Start” section for those of us too impatient to read the whole thing prior to creating our “Hello World” build.  This section holds the “essential elements” of any MSBuild project.  It describes Project files, Properties, Item Groups, Targets, Tasks, the MSBuild command-line and how they relate together all in 18 pages (including code and screen caps).  I really liked this section as it is always difficult teaching these concepts in a linear order since they are interdependent.  You can’t really talk about Tasks until you have introduced Targets, Targets rely on Project files which are run through the MSBuild command-line.  You get the picture.  They give a cohesive view of the MSBuild eco-system, what goes where and how they relate.

Once you have a taste for that, they go on to do “Deep Dive – Part 1” which covers “everything Property” including Reserved Properties, Command-Line Properties, Environment Variables as Properties and Dynamic Properties (sounds like a scene from Forrest Gump…Command-line shrimp, Reserved shrimp, Dynamic shrimp, …).  We then jump into Items, their Metadata and Transformations.  By the end of this you have a good appreciation for how you convey data to your Tasks and Targets. 

The “Deep Dive – Part 2” builds on the prior chapter to go deeper into Dynamic Properties and Items.  One of the best nuggets of the book can be found here.  On pages 60 & 61 they show the "MSBuild Order of Evaluation” which is essential to all but the most trivial MSBuild script.  Up until this point I knew the basics of the evaluation order but had never been able to find a definitive order.  This was the first time I knew that there was much I didn’t know but sadly not the last.

After we finish the MSBuild Deep-dives the book moves onto some more advanced topics, creating and consuming Custom Tasks.  The text walks you through creation of a task, handling Inputs and Outputs both in the Task code and in the MSBuild code and wrapping up any command-line tool in its own Task thus getting better control than the <Exec> task affords.  The chapter ends by showing you how to Debug your Custom Task in Visual Studio.  This last part of the chapter will save you time and frustration when creating your own tasks.

Moving on, we come to a discussion of the Standard Loggers and the creation of Custom Loggers.  As with the Custom Task chapter, you get a thorough treatise on the creation and usage of loggers in MSBuild.  The code samples (which can be downloaded) give you the basis for creating your own loggers or you can just compile and use theirs.

Now we come to Part III, Advanced MSBuild Topics.  This section is broken across 2 chapters, Batching and Incremental Builds and External Tools

Batching is a process whereby MSBuild looks at an Item list and groups its elements based on some metadata attribute’s values.  This is a very powerful feature of MSBuild which differentiates it from tools like NAnt (or other xAnt-based systems).  With Batching we can pass an Item list to a Task and have it loop over the elements performing the Task once per element or once per Metadata attribute value.  Like the section titles says, it’s an Advanced Topic but one that opens the full power of MSBuild up to you.  I have been struggling with understanding all of the ins-and-outs of this feature for a couple of years.  They did a good job of explaining it and now i think I’m finally “getting it”.

MSBuild has an Incremental Build feature which, when configured, will allow the Targets to look at the date/time stamp of the source files and the target binaries and make a decision to compile a project if any of the source files are newer than the target binary (or if one of the binary’s dependencies has been recompiled).  There’s more to it but you get the idea.  MSBuild can also partially build targets.  These optimizations can make a large, unwieldy build more manageable.

The end of this section give advice on how to create reusable MSBuild scripts that let you leverage some build functionality across multiple builds while keeping maintenance to a single script file.  It also shows how to integrate the NUnit testing framework and FxCop code analysis tools into your build, scripts and all.

Sayed ends the MSBuild part of the book with a bang!  Part IV is the MSBuild Cookbook which gives practical applications like Setting the Assembly Version, Building Multiple Projects, Handling Error, Replacing Values in Config files (read: fixing web.configs per environment), Starting and Stopping Services, Encrypting web.config, and a bunch more.  These two chapters lay out many of the common activities you perform in a build process while also showing real world examples of usable Targets and Tasks.

Team Build

After getting a good grounding in MSBuild you are ready to take on Team Build.  This part of the book follow the same pattern as the MSBuild sections by starting with the “Team Build Quick Start”.  When you finish going through this section you will have seen all of the basic pieces of Team Build and ensured that your TFS and Build agent environment is configured correctly.

The “Deep Dive” section is truly “deep”.  It describes the default build process and then shows how you can customize the defaults to bring in your project’s specific steps.  Need to customize the Clean process?  It’s in there.  Want to package your website into a Zip file?  No problem.  All the overridable “hook” Targets that Microsoft left in the process are described along with those Targets that you shouldn’t touch.  Each and every Target in each build step is described along with how to skip them if you want to.  Need to compile a VB6 app?  Just skip the standard Compilation step and add your call to VB6’s compiler in the AfterCompile target.  Now that’s helpful.

It get’s even “deeper”.  What do you do if your build needs to run tests that show a dialog on-screen?  Check out page 310 for a discussion on Running Interactively.  There’s a few pages on configuring SSL communication between the TFS App Tier and your Build Agent.  There’s also a section on Enabling Parallel Builds so that you can take advantage of MSBuild’s ability to use multiple CPU cores to run parts of a build in parallel.  In real terms this means that you can set up your build to run the Debug and Release versions of your app at the same time on a single machine.  This can significantly reduce the overall time of your build.  Good stuff!

Once you have put that stuff to good use you can learn about using the Team Build API which allows you to interrogate TFS and the Team Build services.   From this basis you could create a web page that allows “Build Self-Service” where someone could queue up a build without using Team Explorer.  you could also write an app that balances the build load among multiple Build Agents like the Team Foundation Build Load Balancer.  You could even use it to query the build history and display that information on a build dashboard like the Team Build Screensaver.

Beyond that the Team build section is true to the format laid out earlier.  There is a “Team Build Cookbook” section that goes into detail (down to screenshots and download URLs) on how to hook in Sandcastle for API Documentation generation, how to configure a Build Agent at a remote site to use a TFS Proxy server to speed up the Get portion of the build, and how to add your own custom Build Steps to the Team Build log window. 

Most organizations have a need to package and deploy the outputs of the compilation process.  The section on “Packaging and Deployment” shows you how to hook into Team Build at the appropriate point with your specific packaging steps (the examples Zip up the binaries) and copy them to some location for deployment.  The is a specific distinction between “deployment of the outputs of the build (or the package of them) to a drop location” and “deployment to an environment”.  The examples show how to deploy to the drop folder only although you could use the Zip code, a Copy task and the section on using (the excellent) PsExec tool in MSBuild to create a basic automated installation.

Summary and Opinions

While I may sound like a shill for the authors and publisher, I really do believe what I’ve written.  In fact, I bring the book with me to most of my consulting engagements and recommend it to my clients.  It’s useful when read from cover-to-cover or when used as a reference. 

So go out and buy a bunch of copies and make Sayed and William the first technical authors to get rich off writing a book. ;-)

0 comments:

Post a Comment