How to: Use the Same Target in Multiple Project Files.

Further to my post yesterday about Importing projects in MSBuild, it’s worth having a quick read over the “Order of Evaluation” section to understand further that it is important where you import a project if you wish to set the properties and items of the imported project.(TL;DR -

  1. Define, in the project file, all properties and items that are used as parameters for properties and items in the imported project.
  2. Import the project.
  3. Define in the project file all properties and items that must override default definitions of properties and items in the imported project.
It’s also important to understand that properties and items names are important as the values shall propagate through the imported projects. So if you have the same key value pair in Project A and Project B, and you import Project A then Project B, the value of Project B will be used when running the targets in Project A. This is because all projects and items are collated prior to the build executing anything. If you’re not careful you may unwittingly update a property/item without even realising that you have done so! I learnt this one the hard way: by hard way I mean 6 hours of looking in the wrong imported project file way!