MSBUild task: Get the latest code from TFS and build

Resource

This is a simple task, but it took some time for me to udnerstand, as MSBuild was a new thing to me.
First, Get the latest code from TFS is TFS task, not MSBUild task. So, you can use tf.exe command-line utility.

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" get $/root/MSBUILD/Team/Sapphire /force /recursive

the batch file is like this

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe" get $/root/OccupyingSpace/GoYocal /force /recursive
"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe" "C:\TEMP\Test_GoYocalBuild.proj" /t:Clean;Test
pause

You need SDC taks in order to build .sln file. Please download it from codeplex. I created a simple proj file that builds .sln file

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
    <Import Project="C:\vss\root\MSBUILD\bin\Microsoft.Sdc.Common.tasks"/>
    <Target Name="Test" >
        <Tools.DevEnv
            VisualStudio="9.0"
            Path="C:\VSS\root\OccupyingSpace\GoYocal\GoYocal.sln"
            Config="Release"
            OutputFolder="C:\VSS\root\OccupyingSpace\GoYocal\src\app\bin"
            Clean="true">
        </Tools.DevEnv>
    </Target>

    <PropertyGroup>
        <TFSServerName>http://rbi101:8080</TFSServerName>
        <SourceControlMainDirectory>$/root/OccupyingSpace/GoYocal</SourceControlMainDirectory>
        <WorkspaceName>myworkspace</WorkspaceName>
        <TfCommand>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe"</TfCommand>
        <DevenvCommand>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"</DevenvCommand>
        <WorkingDirectory>c:\</WorkingDirectory>
    </PropertyGroup>
</Project>

After I used the batch file, I realised I could use <Exce Command=”> for any dos command. So, I moved “GetLatest” bit into msbuild proj.
Also, included remote copy to the server. The final codes are like the below

batch file

"C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe" MSBUld_Build.proj /t:GetLatest;Build;Deploy
pause

and msbuild proj file

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<Import Project="C:\MSBUILD\bin\Microsoft.Sdc.Common.tasks"/>

<PropertyGroup>
<TF>"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\tf.exe"</TF>
<TFSourceLocation>$/Occup/GoYocal</TFSourceLocation>
<SolutionRoot>C:\Occu\GoYocal</SolutionRoot>
<RemoteWebRoot>\\TJ103\d$\Occu\WebSites\GoYocal</RemoteWebRoot>
<Copy>xcopy /E /I /R /Y</Copy>
</PropertyGroup>

<Target Name="GetLatest">
<Exec Command="$(TF) get $(TFSourceLocation) /force /recursive /version:T /noprompt" ContinueOnError="true" />
</Target>

<Target Name="Build" >
<Tools.DevEnv VisualStudio="9.0" Path="$(SolutionRoot)\GoYocal.sln" Config="Release" OutputFolder="$(SolutionRoot)\src\app\bin" Clean="true">
</Tools.DevEnv>
</Target>

<Target Name="Deploy">
<Exec Command='net use "x:" "$(RemoteWebRoot)" /u:domain\username pwd' />
<Exec Command='$(Copy) "$(SolutionRoot)\src\app" "x:" '/>
<Exec Command='net use x: /d' />
</Target>

</Project>
MSBUild task: Get the latest code from TFS and build

2 thoughts on “MSBUild task: Get the latest code from TFS and build

  1. Sagar Doshi says:

    Hi,

    I am using TFS 2008 and VS2010.
    My question is: Can we avoid getting the latest code from TFS?

    My MSBuild.proj file has BeforeGet and AfterGet action. But no Get option. Even tough it takes the latest code every time i fire a Automated build.

    Any help will be appreciated.

    Thanks,

  2. Charles Turcotte says:

    Hi,

    Your post is realy helpfull but there is 1 thing you could change. Instead of doing /t:GetLatest;Build;Deploy you could do something like this
    /t:run and in your msbuild doing something like this

    Hope this helped you too

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s