This article is a part of MSBuild series. Here is a list of all articles in this series:
- MSBuild basics for Sitecore devs
- MSBuild extension points
- MSBuild Build and Publish pipelines
- How to extend MSBuild publish pipeline to copy content files from all Helix modules to the output.
- How to extend MSBuild publish pipeline to apply transform files.
- How to extend MSBuild to execute Unicorn Sync action.
- VIDEO: Speed comparison of gulp vs MSBuild build & publish.
- How to extend MSBuild to copy indirect references.
- VIDEO: How to configure local development environment step by step
- VIDEO: How to set up Build & Release pipelines on VSTS step by step (the easiest way)
- How to extend MSDeploy with custom providers for Unicorn and Transform Files
- VIDEO: How to set up Build & Release pipelines on VSTS step by step (to generate WDP package, apply transform files and sync unicorn by MSDeploy)
- Helix Publishing Pipeline by Richard Szalay
Microsoft.Common.targets file defines two main pipelines: Build and Publish. Here is a short summary of both.
The Build pipeline
The newly created
.csproj file contains the following import near the end of the file:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
This file then imports
Microsoft.CSharp.CurrentVersion.targets and it then imports
Microsoft.Common.targets and it then imports
Microsoft.Common.CurrentVersion.targets. On my local PC all files are located here:
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin. If you use a different version of VisualStudio, BuildTools or project type than me, it can load different files.
Microsoft.Common.CurrentVersion.targets contains a definition of all targets and properties related to the Build and Publish process. The most critical target is the
Build target. Its definition looks like this:
<Target Name="Build" DependsOnTargets="$(BuildDependsOn)">
DependsOnTargets attribute says that
Build target depends on targets defined by
BuildDependsOn property. The
BuildDependsOn looks like this:
So it's a list of three targets:
AfterBuild are empty. They are here for us, so we can redefine them (overwrite) in our project files. In other words, we can create a new
.targets file and import it to our
.csproj file, then inside this new
.targets file we can override these targets like this:
<Message Text="Message before build." />
<Message Text="Message after build." />
Alternatively, we can update the value of the
BuildDependsOn property in this way:
BuildDependsOn property now contains a list of 5 targets where the first one and the last one are our custom targets, and the other three are defined in original value of the
BuildDependsOn property. So this is a way to extend a list of targets.
CoreBuild target is defined like this:
<Target Name="CoreBuild" DependsOnTargets="$(CoreBuildDependsOn)">
As you can see it's a long list of targets. There are some preparation steps first, then the references are resolved, and then the code is compiled and so on.
Describing those targets it's outside the scope of this article. It's just a starter. If you want to extend the Build process, you have to dig through it and understand how it works.
<Target Name="Rebuild" DependsOnTargets="$(RebuildDependsOn)">
AfterRebuild are empty, and we can override them.
And again for
<Target Name="Clean" DependsOnTargets="$(CleanDependsOn)">
AfterClean are empty, and we can override them.
The Publish pipeline
Microsoft.Common.CurrentVersion.targets also contains
PublishOnly (used by OneClick toolbar) targets. Here are the definitions:
<Target Name="Publish" DependsOnTargets="$(PublishDependsOn)">
<Target Name="PublishOnly" DependsOnTargets="$(PublishOnlyDependsOn)">
Publish target is executed when you run a Publish in VisualStudio. It first generates manifests, then does a build and then executes
PublishOnly target. When you click Publish button on your OneClick Publish toolbar in VisualStudio, the
PublishOnly target is executed.
AfterPublish targets are empty, and we can override them.
In a web application project, an additional import is added to the project file:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
Microsoft.WebApplication.targets then will import
Microsoft.Web.Publishing.targets. And this is where some customisation to default Publish pipeline, that is required for web applications, is defined.
I want more
To learn more, read my next article in the series, where I describe a real example of extending Publish pipeline.