Adding Intellisense to your custom MSBuild tasks


Using an IDE, especially one like VS.NET 2005, has its faults. You get used to intellisense so much that when that little list doesn’t pop up you feel righteous anger. So, one of the first things I did when I started writing custom tasks for MSBuild was to investigate the possibility of adding intellisense for my tasks. And it turns out that it’s a cakewalk.


 


All you need to do is goto %Program Files%\Microsoft Visual Studio 8\Xml\Schemas\1033 and open up the file Microsoft.Build.xsd. This file has the schema definition for http://schemas.microsoft.com/developer/msbuild/2003.


 


We will extend this schema and let it know about our tasks as well. This is the basic structure of a task definition.


 


    <xs:element name=MyTask substitutionGroup=msb:Task>


        <xs:complexType>


            <xs:complexContent>


                <xs:extension base=msb:TaskType>


                    <xs:attribute name=MyParameter type=xs:boolean use=required/>


                </xs:extension>


            </xs:complexContent>


        </xs:complexType>


    </xs:element>


 


Just substitute the name of your task in the xs:element tag and add the parameters that your task supports as xs:attribute tags


 


Create a separate schema definition file with your custom task descriptions and then include that file in Microsoft.Build.xsd:


<xs:include schemaLocation=MSBuild\MyCustomTasks.xsd/>


 


The custom tasks schema file will start like this


<?xml version=1.0 encoding=utf-8?>


<xs:schema xmlns:msb=http://schemas.microsoft.com/developer/msbuild/2003 elementFormDefault=qualified targetNamespace=http://schemas.microsoft.com/developer/msbuild/2003 xmlns:xs=http://www.w3.org/2001/XMLSchema>


  <xs:include schemaLocation=Microsoft.Build.Core.xsd />


 


And that’s it you are done!


 


In your proj files just ensure that you are using the MSBuild namespace:


<Project xmlns=http://schemas.microsoft.com/developer/msbuild/2003>


 


Start typing the name of your custom task and voila!


Comments (4)

  1. stevew says:

    This seems very easy but I don’t like the idea of having to modify files that are part of the VS.2005 distribution.

    What happens when Microsoft creates a patch that changes Microsoft.Build.xsd?

    Also I now need to have all my developers copy our schema file into their VS.2005 installation. Wouldn’t it have been better if I could create my own schema that references the MSBuild schema and then have my project files reference my schema files. This would allow me to update our schema and developers get it automatically. No further installation is required. Of course it would be possible that our schema could be included directly from source control by Microsoft.Build.xsd but all developers would need to use the same directory or at least the same relative directory for this to work.

  2. Srivatsn says:

    Steve,

    I agree that it feels fishy to be changing files part of the distribution. But I dont see much of a problem if Microsoft releases a patch for the file. We are just adding one include statement in the file.

    The one issue I thought of in creating my own schema is that since the MSBuild schema must be referenced in my proj file, I’ll have to preface every custom task with a namespace reference <myTasks:myTask> and I dont know if MSBuild engine will accept this.

    Like you said you could store your schema in a fixed location and reference it from Microsoft.Build.xsd. This way changes can be made centrally.

  3. Nate says:

    Is it possible to create these definitions and keep them close to your solution (perhaps close enough to be in the same code repo?) and then add references to the proj file using a relative path or something?

  4. Alex says:

    Good question, I don't feel fine about changing visual studio distributed files