MSBuild .targets 文件MSBuild .targets files

MSBuild 包括多个 .targets 文件,文件内容包含常见方案的项、属性、目标和任务。MSBuild includes several .targets files that contain items, properties, targets, and tasks for common scenarios. 这些文件将自动导入到大多数 Visual Studio 项目文件中,以便简化维护,增强可读性。These files are automatically imported into most Visual Studio project files to simplify maintenance and readability.

项目通常会导入一个或多个 .targets 文件以定义它们的生成进程 。Projects typically import one or more .targets files to define their build process. 例如由 Visual Studio 创建的 C# 项目将导入 Microsoft.CSharp.targets ,它可导入 Microsoft.Common.targets 。For example a C# project created by Visual Studio will import Microsoft.CSharp.targets which imports Microsoft.Common.targets. C# 项目本身会定义特定于该项目的项和属性,但 C# 项目的标准生成规则在导入的 .targets 文件中进行定义。The C# project itself will define the items and properties specific to that project, but the standard build rules for a C# project are defined in the imported .targets files.

$(MSBuildToolsPath) 值指定这些公用 .targets 文件的路径 。The $(MSBuildToolsPath) value specifies the path of these common .targets files. 如果 ToolsVersion 为 4.0,则文件位于以下位置:<WindowsInstallationPath>\Microsoft.NET\Framework\v4.0.30319\If the ToolsVersion is 4.0, the files are in the following location: <WindowsInstallationPath>\Microsoft.NET\Framework\v4.0.30319\


若要了解如何创建自己的目标,请参阅目标For information about how to create your own targets, see Targets. 有关如何使用 Import 元素将项目文件插入到其他项目文件的详细信息,请参阅 Import 元素 (MSBuild)如何:在多个项目文件中使用同一目标For information about how to use the Import element to insert a project file into another project file, see Import element (MSBuild) and How to: Use the same target in multiple project files.

公用 .targets 文件Common .targets files

.targets 文件 .targets file 描述Description
Microsoft.Common.targetsMicrosoft.Common.targets 定义 Visual Basic 和 C# 项目标准生成过程中的步骤。Defines the steps in the standard build process for Visual Basic and C# projects.

由 Microsoft.CSharp.targets 和 Microsoft.VisualBasic.targets 文件导入,其中包括以下语句:<Import Project="Microsoft.Common.targets" /> Imported by the Microsoft.CSharp.targets and Microsoft.VisualBasic.targets files, which include the following statement: <Import Project="Microsoft.Common.targets" />
Microsoft.CSharp.targetsMicrosoft.CSharp.targets 定义 Visual C# 项目标准生成过程中的步骤。Defines the steps in the standard build process for Visual C# projects.

由 Visual C# 项目文件 (.csproj) 导入,其中包括以下语句:<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> Imported by Visual C# project files (.csproj), which include the following statement: <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Microsoft.VisualBasic.targetsMicrosoft.VisualBasic.targets 定义 Visual Basic 项目标准生成过程中的步骤。Defines the steps in the standard build process for Visual Basic projects.

由 Visual Basic 项目文件 (.vbproj) 导入,其中包括以下语句:<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" /> Imported by Visual Basic project files (.vbproj), which include the following statement: <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />


Directory.Build.targets 是用户定义的对目录下的项目提供自定义选项的文件 。Directory.Build.targets is a user-defined file that provides customizations to projects under a directory. 除非属性 ImportDirectoryBuildTargets 设为 false,否则该文件将从 Microsoft.Common.targets 自动导入 。This file is automatically imported from Microsoft.Common.targets unless the property ImportDirectoryBuildTargets is set to false. 有关详细信息,请参阅自定义生成For more information, Customize your build.

请参阅See also