Export (0) Print
Expand All

Walkthru:Build System Customization 

Visual Studio 2005
Extensibility Guided Tour Send comments on this topic.
Walkthru:Build System Customization

Glossary Item Box

Build System Customization

Team Foundation Build is an integrated build environment designed to help ensure that development efforts don't get derailed by hidden ‘gotchas' lurking around in un-built and untested code. Team Foundation Build provides an easy to use wizard and supporting UI that allows developers and build managers powerful flexibility in designing, creating, scheduling, and executing builds. With Team System Build, users are free to easily modify their builds, add additional materials into the output, and automate their build process to help ensure better integration of new changes and additions.

Extending Team Build

Team Foundation Build is based on MSBuild, and is therefore governed by a set of XML files that enable fine control over the build process. The XML file that directs the build process is made up of Targets, which define output, guide the build sequence, and serve to group Tasks. Tasks, in turn, represent specific items of work, such as loading files, compiling code, writing output, etc. Build files also provide for another directive, the UsingTask element, that specifies the location of the assembly that will handle the task in question. The UsingTask directive can therefore be leveraged by developers and Visual Studio Industry Partners to integrate new build tasks into the build process.

The main difference between extending Team Foundation Build and MSBuild is that you are changing a Team Project property or asset – which is under source control, meaning that in order to modify the build file, you must check it out and modify it, then check-in your changes. In this walkthrough you will create a very simple custom Task and integrate it into Team Foundation Build.

1.  Start by creating a new Team Project. Select the File | New | Team Project menu option (Note you will need to connect to a Team Foundation Server prior to being able to create a new Team Project). Specify "MyBuildProject" as the name of the Project, then press Next. Select the MSF for Agile Software Development Process Template and click Finish.

2.  In order to be able to use Team Foundation Build, you will need a Project to build. Once the Team Project has been created, create a new Project and add it to the MyBuildProject Team Project by selecting the File | New | Project menu option. Create a new C# Console Application, specify "ConsoleBuildTest" as the Name, and ensure that you check the Add to Source Control check box before clicking OK.

3.  The Add Solution to Source Control dialog will appear. Select MyBuildProject, and click OK.

4.  There is really no need to do anything with the Console Application – close Program.cs. In Solution Explorer, right click on the ConsoleBuildTest Solution and select Check In to commit the files to source control. In the Check In dialog, specify "Initial Checkin" in the Comment field and press Check In.

At this point you are ready to begin creating a new Custom Build Type – a special set of build instructions, or a build file, that will build a specified set of projects within the current Team Project.

5.  In Team Explorer, expand the MyBuildProject node. Right click the Team Build node and select the New Team Build Type menu option to start the New Team Build Type Creation Wizard.

6.  Specify "Sample" as the name of the Build Type, and click Next. In the Select and order solutions to build page, put a check mark in the ConsoleBuildTest Project, and click Next.

Click Next again to specify the Release version of the Project.

7.  Specify the Machine Name of the machine you are currently working on, as well as a build directory and the location of a share, then click Next.

Note: You may need to create a share if you do not already have one created (ensure that either the Everyone or TFS Service user has Full Control of the Share, and ensure that the TFS Service has Modify rights on the directory itself). Contact your Team Foundation Server Administrator if you are having any issues connecting to the Build Service, or in configuring permissions.

8.  Click Next again, and then click Finish on the Summary page. The Team Project will now create a new Build Type based on the information you supplied. Wait while the Build Type is configured (it may take a while). Once completed, a copy of TFSBuild.proj (your Team Project's Build File) will be loaded into the editor. A new build icon will also be added to the Team Builds node.

You are now ready to create a simple Build extension which you can then integrate into your Team Project's build file, or TFSBuild.proj.

9.  Select File | New | Project and create a new Class Library Project with a Name of "CustomBuildTask". Do not add the Project to Source Control. Click OK.

10.  In Solution Explorer, right click on the References node in the CustomBuildTask project and select Add Reference. From the Add Reference dialog, on the .NET tab, scroll down and select the following two Assemblies (hold down the CTRL key to select multiple choices):


Then click OK.

11.  Add the following directives to the top of Class1.cs :

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

12.  Replace Class1 with the following code:

    public class Simple : Task
        public override bool Execute()
            using (StreamWriter sw = new StreamWriter(@"c:\simple.txt"))
                sw.Write("Real Functionality would go here");

            return true;

13.  Save your changes, and select the Build | Build CustomBuildTask menu option. Close Class1.cs.

With a simple Build extension created, you are now ready to integrate it into the current Team Project. To do that, you will need to check out the Team Project's build file from Source Control, make the changes, and then commit them back into source control (each time Team Foundation Build executes it fetches the latest version of the build file from source control).

14  Select the View | Other Windows | Source Control Explorer menu option.

15.  In the Source Control Explorer, navigate to the MyBuildProject node/repository, and then navigate to the Team Build Types | Sample folder. Then right click on the TFSBuild.proj file and select Get Latest Version (which will create a copy of the file on your local server.)

16.  Once the Get Latest Version operation completes, right click TFSBuild.proj again, and this time select the Check Out for Edit menu option. (If the Check Out dialog flashes, just select None, and click Check Out.) Double click on TFSBuild.proj to open the Team Project's build file in the editor.

17.  Take a second to examine the file, then scroll down to the bottom of the file to add a new set of directives that will instruct the build file to implement your custom task the next time it runs. Down below the </ItemGroup> tag, but before the final </Project> tag, insert the following code:

  AssemblyFile="c:\[your path here]\CustomBuildTask.dll"/> 
<Target Name="PostBuild" > 
<Simple /> 

18.  Now replace the [your path here] text with the exact path to your CustomBuildTask.dll. Now take a second and review the code you just added. The first element instructs the Build Engine to load a new Task Type. The name of the Task follows a simple Namespace.ClassName format – and the engine is then provided with an assembly from whence it can load the assembly at run time. Finally, the <Target> tag specifies a fixed build step, or sequence (i.e. an existing Target in Team Foundation Builds), where the Simple Task is to be called. A more extensive example would include arguments for the Simple Task specified as child nodes.

19.  Save your changes to TFSBuild.proj and return to Source Control Explorer. In Source Control Explorer, right click TFSBuild.proj and select Check In Pending Changes. In the Check In dialog, specify "Added Custom Task" as the Comment, and click Check In.

20.  Switch back to Team Explorer, and double click on the Sample node in the Team Builds node. From the Build Manager, click on the Build icon in the top Toolbar to begin building.

21.  From the Build dialog, click the Build button to commence the build process. The build process will provide updated information as the build proceeds.

22.  Once the build process is complete, use Windows Explorer to navigate to your C:\ drive. Once there, locate the simple.txt file created by the Simple task that you created in this walkthrough.

© 2014 Microsoft