John Lam 1997 — 2026

Task and Target Composition in MSBuild

One of the really cool features of any of the popular build systems is the ease with which tasks can be composed. MSBuild takes it to the next level with the ability to consume and emit Items, which decouples tasks further (arguably completely), and by extension, makes it easier to compose their functionality.

I find it particularly interesting that once you start writing more complex MSBuild projects that you wind up composing Targets in addition to composing Tasks. This lets you take advantage of the inter-target dependency engine and lets you switch Targets at run-time by Importing different .targets files.

If you look at the set of *.targets files that ship with V1.2 of the frameworks (CSharp.targets, framework.targets etc.) you find a lot of Targets that have only a single task within them (e.g. BuildProjectReferences, CopyProjectReferences, Compile etc.). It’s interesting that most composable targets contain only a single task, however there are more than enough multi-task targets to justify pushing the composition into the targets level.

Are there other folks who are building their own sets of *.targets files, or are most folks using the MS-supplied *.targets files? I’m curious about what other kinds of general-purpose *.targets files people have come up with to serve their own needs.

1 archived reader comment

Lars Bergstrom (2003-12-12)

On my project (internal to MS right now), we have a custom .targets file to add all of our custom tasks and targets as well as unify our build output directory and get the intermediates out of the source tree. We include the default one from the MSBuild team within our custom file.

Some of the custom tasks I’ve written include an NUnit test runner and a variety of code generators for specific data files that we have in our project (localized strings -> strongly typed accessors, some random XML -> a bunch of code, etc.).