Home > 2010, Team Build, TFS, TFS2010, Visual Studio > Pass Relative Path Arguments to MSBuild in TFS2010 Team Build

Pass Relative Path Arguments to MSBuild in TFS2010 Team Build


If you are familiar with the Team Build process in TFS2010, you can skip the background section.

Background

As we know that in TFS2010, the team build leverages workflow instead of build scripts. We will also notice that TFSbuild.proj no longer exists. We can find the build template at the BuildProcessesFolder Open the DefaultTemplate.xaml, and expand the workflow. Then find the MSBuild activity Hit F4 or open the Properties Window. Notice the CommandLineArguments property. The value of that property is passed to MSBuild process as argument (i.e. MSbuild.exe /p:SkipInvalidConfigurations= true /p:referencepath=somepath …. Notice the MSBuildArguments in the value field. The MSBuildArguments is an Argument that is built into the Default Team Build template. If you click the Arguments button at the bottom, you will find it listed there. Those arguments are properties in your team build definition. Right click on the team build definition, click Edit Build definition

Select the Process tab from the left tab list. Notice that the Build process parameters are listed in the arguments list in the build template. Keep in mind, only the arguments with the “In” or “In/out” direction are visible to the process template

 

In a solution that contains many projects, it is normal to have a folder that contains common references to third party assemblies. Those assemblies can be custom controls, enterprise libraries, reusable libraries from other projects, ..etc. The Msbuild needs to have a reference to that folder to be able to compile the project or the solution. You need to keep in mind the following points:

  • Team build downloads the latest files from source control every time team build is requested; custom assemblies need to be checked in
  • The folder need to be visible to the teambuild workspace; in build definitionworkspace
  • Typically you have multiple build agents whose working directory path is different from the development workstation and other build agents. In other words, you can’t pass an absolute path to msbuild

Problem

We want to pass a relative path as MSBuild argument i.e. MSBuild /p:referencepath=”$(SourceDirectory)\referenced assemblies\”. Team build template doesn’t out of the box convert $(SourceDirectory) to a physical path when passed as MsBuild argument

Solution

We need to modify the build template to replace $(SourceDirectory) with the source directory’s physical path.

  • Add an If activity to check if the MSBuildArguments is null or empty. To keep things organized, I recommend adding the activity to the “Run on Agent”Initialize Variables. Make sure you add it at the end because the source directory is initialized in that block
  • Add an Assign Activity to the Then block.

  • Open the properties window
  • Give the activity a more descriptive name
  • In the To property, type MSBuildArguments
  • In the value property, type MSBuildArguments.Replace(“$(SourceDirectory)”, SourcesDirectory). This is case sensitive.

  • Save the template
  • Check in the template

  • Open an existing build definition or create a new one
  • Go to the process section
  • Expand the advanced section
  • Enter /p:referencepath=”$(SourceDirectory)\Referenced Assemblies\\” in the MsBuild Arguments parameter, where “Referenced Assemblies” is the folder where your assemblies are saved. Don’t forget the quotes and the double slash that the end (i.e. \\). Thanks to  Bernd who commented on the this post. I was able to confirm if you have spaces in your path you need to either add double slash at the end of the path or no slash characters at all (e.g. “$(SourceDirectory)\Referenced Assemblies”)

  • Save the definition and queue a new build
  • The following is a screen show of the log

About these ads
  1. Bernd
    March 16, 2011 at 8:45 am

    Hi!

    Thanks for this great and very detailed article. I have one little comment. In case you should have a space somewhere in your base path (SourcesDirectory) your solution does not quite work. In that case you must use double back-slashes at the end such as:
    /p:”$(SourcesDirectory)\Referenced Assemblies\\” instead of /p:”$(SourcesDirectory)\Referenced Assemblies\”

    • Lajak Technologies Inc
      March 16, 2011 at 8:55 am

      Thanks for the complement. Thanks for the info. I will verify it tonight and update the post.

      Thanks again
      Ahmed

  2. Mauricio
    May 3, 2011 at 3:50 pm

    brilliant,

    thank you guys. i got this to work

    mauricio

    • Lajak Technologies Inc
      May 3, 2011 at 4:00 pm

      Glad that I was able to help :)

  3. Craig Richards
    May 30, 2011 at 4:56 pm

    Thanks for the article, this solved something we had been working on for days!!!

    Cheers
    Craig

    • Lajak Technologies Inc
      May 30, 2011 at 7:21 pm

      Really happy for u. Let me know if u need further help
      Cheers
      Ahmed

  4. sheir
    November 29, 2011 at 1:34 pm

    I am using NANT and msbuild on a C# project file and am having issues with reference path.
    snippet of my nant script:

    Problem is the referencePath blows up if I add the 2nd path, which I need to.

    Output error:
    [echo] Building ProductionPLUS project solution (release)
    [exec] Starting ‘MSBuild.exe (C:\Projects\sali_CMIWKS217\Apps\MAIN\ProductionPLUS\ProductionPLUS.csproj /m /t:build /p:Configuration=release “/p:Platform=Any CPU” /p:OutputPath=bin\release /p:TargetFrameworkVersion=v3.5 /p:ReferencePath=C:\Projects\sali_CMIWKS217\Apps\MAIN\CommonModules\bin\Obfuscated;C:\Projects\sali_CMIWKS217\Apps\MAIN\CommonModules\bin)’ in ‘C:\Projects\sali_CMIWKS217\Apps\MAIN\Build\nAntBuildScripts\ProductionPlusSDK’
    [exec] Microsoft (R) Build Engine Version 3.5.30729.5420
    [exec] [Microsoft .NET Framework, Version 2.0.50727.5448]
    [exec] Copyright (C) Microsoft Corporation 2007. All rights reserved.
    [exec]
    [exec] MSBUILD : error MSB1006: Property is not valid.
    [exec] Switch: C:\Projects\sali_CMIWKS217\Apps\MAIN\CommonModules\bin
    [exec]
    [exec] For switch syntax, type “MSBuild /help”

    Any idea on what I am doing incorrectly?

    • Lajak Technologies Inc
      December 6, 2011 at 1:10 pm

      I am sorry I have never used NANT before

    • Lajak Technologies Inc
      December 6, 2011 at 1:14 pm

      Are you missing double quotes?
      /p:”ReferencePath=C:\Projects\sali_CMIWKS217\Apps\MAIN\CommonModules\bin\Obfuscated;C:\Projects\sali_CMIWKS217\Apps\MAIN\CommonModules\bin”

  5. sheir
    December 6, 2011 at 1:33 pm

    Seems I had to repeat the ReferencePath twice in the nant build script.
    Such as:

    That worked.

    • sheir
      December 6, 2011 at 1:35 pm

      How to post nant script code in a reply as wordpress blog removes it all????

      • sheir
        December 6, 2011 at 1:38 pm
        		<exec program="MSBuild.exe" resultproperty="res" failonerror="true">
        			<arg value="${ProductionPlusDir}\ProductionPLUS.csproj" />
        			<arg line="/m" />
        			<arg line="/t:build" />
        			<arg line="/p:Configuration=${buildConfig}" />
        			<arg value="/p:Platform=Any CPU" />
        			<arg line="/p:OutputPath=bin\${buildConfig}" />
        			<arg value="/p:TargetFrameworkVersion=v3.5" />
        			<arg value="/p:ReferencePath=${baseDir.parent}\CommonModules\bin" />
        			<arg value="/p:ReferencePath=${baseDir.parent}\CommonModules\bin\Obfuscated" />
        		</exec>
        
  6. Lajak Technologies Inc
    December 6, 2011 at 1:42 pm

    Sweet, you got it to work :) I was gonna ask you to follow this

    http://en.support.wordpress.com/code/posting-source-code/

    but I guess you figured it out :)

  7. February 21, 2013 at 11:28 am

    Very very helpful, many thanks!

    (Time and time again surprised about how such important features (variable replacement) are missing – and have been missing from a product like Visual Studio (output path with e. g. $(ConfigurationName) makes sense in so many scenarios) for generations.)

    • Lajak Technologies Inc
      February 21, 2013 at 12:55 pm

      This is one of the top viewed posts in my blog :)
      Happy it helped you out.

  8. Arno
    May 14, 2013 at 4:08 am

    Brilliant article, you made my day! Now we can include our third party references.

  1. No trackbacks yet.

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

Follow

Get every new post delivered to your Inbox.

Join 352 other followers

%d bloggers like this: