Add Ons - DevSource
DevSource: Microsoft Developer Resource DevSource Home Sponsored by Microsoft Home Add Ons Architecture Languages Techniques Using VS Forums
Home arrow Add Ons arrow Page 2 - Automation with MSBuild
Automation with MSBuild
By Steve Andrews

Rate This Article: Add This Article To:

Automation with MSBuild - Tasks and Command Line
( Page 2 of 2 )

Third-Party Tasks

The built-in tasks provide the basic functionality to build .NET projects, but most likely you will need to do more. Developing for SharePoint, for example, requires building special files and using those files to create a SharePoint package for deployment. The same is true for BizTalk, which can also require GAC registration and BizTalk utility calls. Even if you aren’t developing for these products, you may need to build an MSI file, create IIS directories, create SQL Server databases, or one of a myriad of other tasks.

ADVERTISEMENT

On the web are several free task collections you can download which include tasks for working with SharePoint, BizTalk and IIS as well as work with Active Directory, SQL Server, zipping files, the registry, and plenty more.

To learn more and download these custom tasks, visit the following sites:

  1. SDC Tasks - http://www.codeplex.com/sdctasks
  2. Tigris Tasks - http://msbuildtasks.tigris.org/

Custom Tasks

Sometimes the built-in and third-party tasks don’t cover the functionality you need. The third option for tasks is to create your own. MSBuild provides an API for you to be able to build your own custom tasks.

There are two ways of creating custom tasks. The easiest method is to inherit from Microsoft.Build.Utilities.Task and override the Execute method. The Execute method takes no parameters, and returns a boolean value indicating the success or failure of the task. Inside the execute method, you are at liberty to include whatever code is necessary to perform the task at hand. The recommended approach is to encapsulate your code inside of a try/catch block and use the Log methods to ensure traceability and debugging.

To make things easier for you when creating custom MSBuild tasks, I recommend using the following class stub:

using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;

public class CustomTask : Task
{
    private string _myProperty;
 
    public override bool Execute()
    {
        try
        {
            Log.LogMessage("Executing CustomTask for " + _myProperty);
 
            // EXECUTE TASK HERE
 
            Log.LogMessage("Execution of CustomTask succeeded for " +
                _myProperty);
 
            return true;
        }
        catch (Exception ex)
        {
            Log.LogMessage("Execution of CustomTask failed for " + 
                _myProperty + ". Reason: " + ex.Message);             Log.LogError(ex.Message);             return false;         }     }       [Required]     public string MyProperty     {         get         {             return _myProperty;         }         set         {             _myProperty = value;         }     } }

The second, and more customizable method is to implement the Microsoft.Build.Framework.ITask interface. The abstract Task class inherited in the first example itself inherits from ITask under the covers and handles a lot of the implementation details for you.

public interface ITask
{
    IBuildEngine BuildEngine { get; set; }
    ITaskHost HostObject { get; set; }
    bool Execute();
}

Command Line Execution

The MSBuild utility itself is a command line application. As such, there are various parameters that you can use to control how the MSBuild command line utility behaves. The most common parameters include:

/targets:<targets>Specifies the targets to build in the project file.
/property:<n>=<v>Sets or overrides project properties. You can use a semicolon or a comma to separate multiple properties, or specify each property individually.
/verbosity:<level>Controls how much information is written to the event log. The available levels are quiet, minimal, normal, detailed and diagnostic.
/logger: [<logger class>,]<logger assembly>[;<logger parameters>]Specifies a logger to log events from MSBuild. To specify multiple loggers, specify each logger separately.
/nologoPrevents the program information header from being displayed or written to the log file.

For a full list of parameters, you can type msbuild /? in the Visual Studio command prompt.

Logging

When using batch files, especially in an automated environment, it can be very hard to track and diagnose errors when they occur. In MSBuild, error logging is built-in, thereby resolving this problem. By default, MSBuild will output a message for each target and task that gets executed, so you can follow the entire process from end to end.

When running an MSBuild project file from the command line, you can specify the /log switch and provide the path for a log file. You can also use the built-in Message task to force log messages to be written for debugging or diagnostic purposes.

Conclusion

In this article, you were introduced to the basics of using MSBuild. MSBuild provides the capability to create reusable, dynamic and flexible build and deployment scripts using the full IntelliSense and color-coding in Visual Studio. Using MSBuild, we can finally get away from batch files and one-off applications and their shortcomings.

Additional Resources

To learn more about MSBuild, visit the following resources:

  1. MSBuild Reference on MSDN
  2. http://msdn2.microsoft.com/en-us/library/0k6kkbsd.aspx
  3. MSBuild Team Blog
  4. http://blogs.msdn.com/msbuild/
  5. Visual Studio MSBuild on the MSDN Forums
  6. http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=27&SiteID=1


 
 
>>> More Add Ons Articles          >>> More By Steve Andrews
 



Microsoft's Future: A Chat With Their CTO, Barry Briggs

Play Video >

All Videos >

Julia explores the Robotics Studio!

Read now >

Messages to Bill Gates!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.