Properly incorporate MsBuild arguments into your build process template

Ever wondered how to put features like Architecture Layer validation into your builds? If you bingle it you'll find loads of posts that tell you which argument to pass to msbuild. The official MSDN docs for this feature can be found here.

Basically, the trick is to either edit your Visual Studio Modelling project to always do layer validation, but that then also happens on the client unless you add a couple of conditions to it:

<!-- Use $(BuildingInsideVisualStudio) or $(TeamBuildConstants) to detect building inside Team Build -->
<PropertyGroup Condition=" '$(BuildingInsideVisualStudio)' == '' "> 

Or to to add the following argument to the commandline:


But I prefer this feature to be available from the Advanced tab of the Queue build window, and to be able to use the same options as Code Analysis: Always, Never, AsConfigured. That way you can either force Architecture Validation to run for all projects, or to disable it for CI builds for example.

This is relatively simple to do by editing the build workflow. The same trick can be used to add other features, such as the NuGet package source from my previous post.

Open Visual Studio and navigate to the '$/Your team Project/BuildProcessTemplates' folder. Open the build process template you want to add this option to by doubleclicking it (make sure you have the latest version locally).

Add a build argument to the workflow and call it 'RunLayerValidation'. As type select 'Microsoft.TeamFoundation.Build.Workflow.Activities.CodeAnalysisOption', this will add a dropdown with the Never, Always, AsConfigured options.

Set the default value to "never" and the parameter type to "in":

Now add the same argument to the Metadata to give it a proper description and define whether this option is editable in the Queue new Build window. Open the Metadata section on the Arguments tab:

Add the parameter and set the category, description etc:

You can choose whether you want people to edit this property upon queue:

Now you'll need to add a workflow activity that appends the right argument to the MsBuild arguments. Drag a switch statement to the workflow, just before the 'for each project' loop:

As type select the same 'CodeAnalysisOption' which we selected as the ArgumentType:

In the 'Always' and 'Never' case add an Assign activity:

Use the following assignment for "Never":

MSBuildArguments &amp; " /p:ValidateArchitecture=false"

And this one for "Always":

MSBuildArguments &amp; " /p:ValidateArchitecture=true"

You can leave the AsConfigured empty, that way it will use whatever option was configured in the project file.

Check in the build process template xaml and when you edit or queue a build you'll see the "Run Architecture Layer Validation" option.