Import Element (MSBuild)
Imports the contents of one project file into another project file.
The following sections describe attributes, child elements, and parent elements.
The path of the project file to import.
Condition to be evaluated. For more information, see.
Required root element of an MSBuild project file.
The Import element allows you to reuse the code that is common to many project files. Moving common blocks of code into other project files allows you to maintain code and propogate changes in a central location. For example, Project A and Project B set the specific item and property values for their builds, and Project C contains the common build processes. Project A and Project B can import Project C, and any changes to the build processes are only made to one file.
Common imported project files are saved as .targets files by convention, but are standard MSBuild project files. MSBuild does not prevent you from importing a project with a different file extension, but the .targets file extension is recommended for consistency.
All relative paths in imported projects are interpreted relative to the directory of the importing project. Therefore, if a project file is imported into several project files in different locations, the relative paths in the imported project file will be interpreted differently for each importing project.
All MSBuild reserved properties that relate to the project file, for example, MSBuildProjectDirectory and MSBuildProjectFile, that are referenced in an imported project are assigned values based on the importing project file.
If the importing project does not have a DefaultTargets attribute, imported projects are inspected in the order that they are imported, and the value of the first discovered DefaultTargets attribute is used. For example, if ProjectA imports ProjectB and ProjectC (in that order), and ProjectB imports ProjectD, MSBuild will first look for DefaultTargets specified on ProjectA, then ProjectB, then ProjectD, and finally ProjectC.
The schema of an imported project is identical to that of a standard project. It is possible that MSBuild can build an imported project but the build will probably fail because the imported project does not usually contain information about which properties to set or the order in which to run targets. The imported project depends on the project into which it is imported to provide that information.
The following code example shows a project setting several items and properties and importing a general project file.
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <resourcefile>Strings.resx</resourcefile> <compiledresources> $(O)\$(MSBuildProjectName).Strings.resources </compiledresources> </PropertyGroup> <ItemGroup> <CSFile Include="*.cs" /> <Reference Include="System" /> <Reference Include="System.Data" /> </ItemGroup> <Import Project="$(CommonLocation)\General.targets" /> </Project>