Monday, 20 February 2012

TFS2010: Publishing solution projects to their own directories

When looking to automate a TFS2010 build one of the first issues that most people seem to encounter is that all the binaries of each project in a solution end up in the same "bin" directory. The forum post TFS 2010 BUILD SERVER: Can not keep folder tree in the drop location ? details the solution; which is changes to both the CSPROJ file and the workflow template that is called by your build. Note: each CSPROJ file in your project needs to be updated as the workflow loops through the solution finding all the referenced projects.. The answer in the forum post has everything about what is needed but not why, which can be a bit confusing if you're just starting out with TFS / workflow.

The image to the left is the section of the workflow that is changed. The workflow variable "OutputDirectory" is defined within the scope of "Compile and Test for configuration" (highlighted in green). The value of "outputDirectory" is assigned (highlighted in red) and typically just includes the the build name + the build version (so Trunk_20120220.1 would be the first time the trunk build has run on 20th Feb 2012). For the default template the value of "outputDirectory" is assigned to the input argument "OutDir" of the "Run MSBuild for project" step (highlighted in blue). In the default template it is for this reason why all the binaries of all the projects end up in a single directory. The first documented change is to modify the properties of "Run MSBuild for project", the value of "outputDirectory" is no longer passed in the input argument "OutDir" but is assigned to the input argument of "CommandLineArguments" instead.

The code below duplicates the change to the "CommmandLineArguments" input argument referred to in the forum post but highlights the interaction of "OutputDirectory". The value of which is passed as the value of a variable whose name is not important - as long as it matches the reference you add to your CSPROJ file.

String.Format(
"/p:SkipInvalidConfigurations=true {0} /p:TfsProjectPublishDir=""{1}""",
MSBuildArguments, outputDirectory)

The code below shows the corresponding change made to the CSPROJ file. Similar to the previous section the important interaction with the workflow changes are highlighted. The name you use can be any unique variable name that you like that does not clash with existing workflow / environment variables. The change does need to be made to all CSPROJ files and for all configurations that you need to build. In this instances we can build both debug and/or release and we will end up with separate binary directories for each. For each CSPROJ file change the projectname value to a value unique to the project in question.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
...
<OutputPath Condition=" '$(TfsProjectPublishDir)'=='' ">bin\debug\</OutputPath>
<OutputPath Condition=" '$(TfsProjectPublishDir)'!='' ">$(TfsProjectPublishDir)\projectname\</OutputPath>
...
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
...
<OutputPath Condition=" '$(TfsProjectPublishDir)'=='' ">bin\release\</OutputPath>
<OutputPath Condition=" '$(TfsProjectPublishDir)'!='' ">$(TfsProjectPublishDir)\projectname\</OutputPath>
...
</PropertyGroup>

If you're not familiar with CSPROJ files, they are just XML based files that MSBuild uses to build the referenced project. The "Condition" attribute determines whether that XML element (and any children) are passed to MSBuild. So the first "PropertyGroup" is only present if configuration and platform equal Debug and AnyCPU (respectively), the second if it is equal to Release and AnyCPU. As should be apparent this conditional logic implies that only one of these PropertyGroups should ever be presented to MsBuild and they contain the configuration/platform specific values needed for the build. This conditional logic is how MsBuild behaviour changes between the IDE and TFS. If TfsProjectPublishDir is defined then that "OutputPath" node is included, otherwise the default more familiar "OutputPath" node is presented to MsBuild.

No comments:

Post a Comment