When MSBuild reaches an Import element, the imported project is effectively inserted into the importing project at the location of the Import element. Therefore, the location of the Import element can affect the values of properties and items. It is important to understand the properties and items that are set by the imported project, and the properties and items that the imported project uses.
When the project builds, all properties are evaluated first, followed by items. For example, the following XML defines the imported project file MyCommon.targets:
<Project xmlns=”http://schemas.microsoft.com/developer/msbuild/2003”>
<PropertyGroup>
<Name>MyCommon</Name>
</PropertyGroup>
<Target Name="Go">
<Message Text="Name="$(Name)""/>
</Target>
</Project>
The following XML defines MyApp.proj, which imports MyCommon.targets:
<Project
DefaultTargets="Go"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Name>MyApp</Name>
</PropertyGroup>
<Import Project="MyCommon.targets"/>
</Project>
When the project builds, the following message is displayed:
Name="MyCommon"
Because the project is imported after the property Name has been defined in MyApp.proj, the definition of Name in MyCommon.targets overrides the definition in MyApp.proj. If, the project is imported before the property Name is defined, the build would display the following message:
Name="MyApp"
Use the following approach when importing projects
Define, in the project file, all properties and items that are used as parameters for properties and items in the imported project.
Import the project.
Define in the project file all properties and items that must override default definitions of properties and items in the imported project.