Someone asked a question about how to do something in MSBuild the other day. They were using the UpgradeTemplate.xaml, so there build process was done almost entirely in MSBuild. What they wanted to do was fairly simple to do in a Task, but they didn't really want the overhead of creating and maintaining a Task assembly. So, I suggested an inline Task (new to MSBuild 4.0). Of course, the next question was, how can I use that with TFS. So, I started playing around and created this extremely simple inline task that prints out the Start Time of the build by reading it from the server.
This is a very simplified TfsBuild.proj that doesn't actually build anything - I took out the SolutionsToBuild and the ConfigurationsToBuild stuff. The TfsBuild.proj file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<!-- Team Foundation Build Version - DO NOT CHANGE -->
<Reference Include="mscorlib" />
<Reference Include="Microsoft.TeamFoundation.Build.Client" />
<Reference Include="Microsoft.TeamFoundation.Build.Common" />
<Reference Include="Microsoft.TeamFoundation.Client" />
<Using Namespace="System" />
<Using Namespace="Microsoft.TeamFoundation.Build.Client" />
<Using Namespace="Microsoft.TeamFoundation.Build.Common" />
<Using Namespace="Microsoft.TeamFoundation.Client" />
<Code Type="Fragment" Language="cs">
Log.LogMessage("parameters to PrintBuild", MessageImportance.High);
Uri serverUri = TfsTeamProjectCollection.GetFullyQualifiedUriForName(TeamFoundationServerUrl);
// Attempt authentication early. On failure, this is where a good error message will be logged
TfsTeamProjectCollection collection = new TfsTeamProjectCollection(serverUri);
IBuildServer buildServer = (IBuildServer)collection.GetService(typeof(IBuildServer));
IBuildDetail buildDetail = buildServer.GetBuild(new Uri(BuildUri));
<PrintBuild TeamFoundationServerUrl="$(TeamFoundationServerUrl)" BuildUri="$(BuildUri)" />
The blue highlighted part is where the Task is defined. If you want more information on the parts and pieces of the inline Task, see this article.
The yellow highlighted part is where the Task is called. Notice that I put it in an override of the BeforeEndToEndIteration. If you aren't familiar with editing a TfsBuild.proj file, this won't make any sense. But this is just a hook to put my targets into the overall build process. In this case, my targets executes very early in the process.
Here is the output in the log:
Build started 8/22/2011 2:14:07 PM.
Project "C:\Builds\1\Project One\TestMsBuild\BuildType\TFSBuild.proj" on node 1 (EndToEndIteration target(s)).
Project file contains ToolsVersion="3.5". This toolset is unknown or missing. You may be able to resolve this by installing the appropriate .NET Framework for this toolset. Treating the project as if it had ToolsVersion="4.0".
GetBuildProperties TeamFoundationServerUrl="http://localhost:8080/tfs/DefaultCollection" BuildUri="vstfs:///Build/Build/8"
parameters to PrintBuild
8/22/2011 2:13:52 PM
I know this was a short and useless example, but you can easily see how to Get the TFS url and the Build object. This should be enough to get you any information you need.
Good Luck and happy coding!