Project 元素 (MSBuild)Project element (MSBuild)

MSBuild 项目文件必需的根元素。Required root element of an MSBuild project file.

语法Syntax

<Project InitialTargets="TargetA;TargetB"
         DefaultTargets="TargetC;TargetD"
         TreatAsLocalProperty="PropertyA;PropertyB"
         ToolsVersion=<version number>
         Sdk="name[/version]"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Sdk... />
    <Choose>... </Choose>
    <PropertyGroup>... </PropertyGroup>
    <ItemGroup>... </ItemGroup>
    <Target>... </Target>
    <UsingTask.../>
    <ProjectExtensions>... </ProjectExtensions>
    <Import... />
</Project>

特性和元素Attributes and elements

下列各节描述了特性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.

特性Attributes

特性Attribute 描述Description
DefaultTargets 可选特性。Optional attribute.

如果未指定目标,则默认目标将作为生成的入口点。The default target or targets to be the entry point of the build if no target has been specified. 使用分号 (;) 分隔多个目标。Multiple targets are semi-colon (;) delimited.

如果未在 DefaultTargets 属性或 MSBuild 命令行中指定默认目标,那么引擎会在评估 Import 元素后在项目文件中执行第一个目标。If no default target is specified in either the DefaultTargets attribute or the MSBuild command line, the engine executes the first target in the project file after the Import elements have been evaluated.
InitialTargets 可选特性。Optional attribute.

会在 DefaultTargets 属性中或命令行上指定的目标前运行初始目标。The initial target or targets to be run before the targets specified in the DefaultTargets attribute or on the command line. 使用分号 (;) 分隔多个目标。Multiple targets are semi-colon (;) delimited. 如果多个导入文件定义 InitialTargets,则将按照导入的顺序运行所有提到的目标。If multiple imported files define InitialTargets, all targets mentioned will be run, in the order the imports are encountered.
Sdk 可选特性。Optional attribute.

用于创建添加到 .proj 文件的隐式 Import 语句的 SDK 名称和可选版本。The SDK name and optional version to use to create implicit Import statements that are added to the .proj file. 如果不指定任何版本,则 MSBuild 将尝试解析默认版本。If no version is specified, MSBuild will attempt to resolve a default version. 例如,<Project Sdk="Microsoft.NET.Sdk" /><Project Sdk="My.Custom.Sdk/1.0.0" />For example, <Project Sdk="Microsoft.NET.Sdk" /> or <Project Sdk="My.Custom.Sdk/1.0.0" />.
ToolsVersion 可选特性。Optional attribute.

用于确定 $(MSBuildBinPath) 和 $(MSBuildToolsPath) 的值的工具集 MSBuild 的版本。The version of the Toolset MSBuild uses to determine the values for $(MSBuildBinPath) and $(MSBuildToolsPath).
TreatAsLocalProperty 可选特性。Optional attribute.

不会被视为全局的属性名称。Property names that won't be considered to be global. 此属性可防止特定命令行属性替代项目或目标文件和所有后续导入中设置的属性值。This attribute prevents specific command-line properties from overriding property values that are set in a project or targets file and all subsequent imports. 使用分号 (;) 分隔多个属性。Multiple properties are semi-colon (;) delimited.

通常,全局属性会替代项目或文件中设置的属性值。Normally, global properties override property values that are set in the project or targets file. 如果该属性在 TreatAsLocalProperty 值中列出,那么全局属性值不会替代在该文件或任何后续导入中设置的属性值。If the property is listed in the TreatAsLocalProperty value, the global property value doesn't override property values that are set in that file and any subsequent imports. 有关详细信息,请参阅如何:使用不同选项生成相同的源文件For more information, see How to: Build the same source files with different options. 注意: 可使用“-property”(或“-p”)开关,在命令提示符处设置全局属性 。Note: You set global properties at a command prompt by using the -property (or -p) switch. 还可使用 MSBuild 任务的 Properties 属性为多项目生成中的子项目设置或修改全局属性。You can also set or modify global properties for child projects in a multi-project build by using the Properties attribute of the MSBuild task. 有关详细信息,请参阅 MSBuild 任务For more information, see MSBuild task.
xmlns 可选特性。Optional attribute.

指定后,xmlns 属性必须具有 http://schemas.microsoft.com/developer/msbuild/2003 值。When specified, the xmlns attribute must have the value of http://schemas.microsoft.com/developer/msbuild/2003.

子元素Child elements

元素Element 说明Description
ChooseChoose 可选元素。Optional element.

评估子元素,选择一组 ItemGroup 元素和/或 PropertyGroup 元素进行评估。Evaluates child elements to select one set of ItemGroup elements and/or PropertyGroup elements to evaluate.
导入Import 可选元素。Optional element.

启用项目文件,导入另一项目文件。Enables a project file to import another project file. 项目中可能有零个或零个以上的 Import 元素。There may be zero or more Import elements in a project.
ImportGroupImportGroup 可选元素。Optional element.

包含在可选条件下进行分组的 Import 元素的集合。Contains a collection of Import elements that are grouped under an optional condition.
ItemGroupItemGroup 可选元素。Optional element.

各个项的分组元素。A grouping element for individual items. 使用 Item 元素指定项。Items are specified by using the Item element. 项目中可能有零个或零个以上的 ItemGroup 元素。There may be zero or more ItemGroup elements in a project.
ItemDefinitionGroupItemDefinitionGroup 可选元素。Optional element.

可定义一组项定义,这些项定义默认为应用到项目中的所有项的元数据值。Lets you define a set of Item Definitions, which are metadata values that are applied to all items in the project, by default. ItemDefinitionGroup 取代使用 CreateItem 任务和 CreateProperty 任务。ItemDefinitionGroup supersedes the need to use the CreateItem task and the CreateProperty task.
ProjectExtensionsProjectExtensions 可选元素。Optional element.

提供在 MSBuild 项目文件中保留非 MSBuild 信息的方法。Provides a way to persist non-MSBuild information in an MSBuild project file. 项目中可能有零个或一个 ProjectExtensions 元素。There may be zero or one ProjectExtensions elements in a project.
PropertyGroupPropertyGroup 可选元素。Optional element.

各个属性的分组元素。A grouping element for individual properties. 使用 Property 元素指定属性。Properties are specified by using the Property element. 项目中可能有零个或零个以上的 PropertyGroup 元素。There may be zero or more PropertyGroup elements in a project.
SDKSdk 可选元素。Optional element.

引用 MSBuild 项目 SDK。References an MSBuild project SDK. 此元素可以用作 SDK 属性的替代属性。This element can be used as an alternative to the Sdk attribute.
TargetTarget 可选元素。Optional element.

包含一组要连续执行的 MSBuild 任务。Contains a set of tasks for MSBuild to sequentially execute. 使用 Task 元素指定任务。Tasks are specified by using the Task element. 项目中可能有零个或零个以上的 Target 元素。There may be zero or more Target elements in a project.
UsingTaskUsingTask 可选元素。Optional element.

提供在 MSBuild 中注册任务的方法。Provides a way to register tasks in MSBuild. 项目中可能有零个或零个以上的 UsingTask 元素。There may be zero or more UsingTask elements in a project.

父元素Parent elements

无。None.

另请参阅See also