HOW TO:指定要優先建置的目標How to: Specify which target to build first

專案檔可以包含一或多個 Target 項目來定義專案的建置方式。A project file can contain one or more Target elements that define how the project is built. 除非專案檔內含 DefaultTargets 屬性、InitialTargets 屬性,或在命令列中使用 -target 參數來指定目標,否則 Microsoft Build EngineMicrosoft Build Engine (MSBuildMSBuild) 引擎會建置它找到的第一個專案以及任何相依性。The Microsoft Build EngineMicrosoft Build Engine (MSBuildMSBuild) engine builds the first project it finds, and any dependencies, unless the project file contains a DefaultTargets attribute, an InitialTargets attribute, or a target is specified at the command line using the -target switch.

使用 InitialTargets 屬性Use the InitialTargets attribute

Project 項目的 InitialTargets 屬性會指定優先執行的目標,即使已在命令列上或 DefaultTargets 屬性中指定目標也一樣。The InitialTargets attribute of the Project element specifies a target that will run first, even if targets are specified on the command line or in the DefaultTargets attribute.

指定一個初始目標To specify one initial target

  • Project 項目的 InitialTargets 屬性中,指定預設的目標。Specify the default target in the InitialTargets attribute of the Project element. 例如:For example:

    <Project InitialTargets="Clean">

    您可以在 InitialTargets 屬性中指定多個初始目標,方法是依序列出目標,然後使用分號來分隔每個目標。You can specify more than one initial target in the InitialTargets attribute by listing the targets in order, and using a semicolon to separate each target. 清單中的目標將會循序執行。The targets in the list will be run sequentially.

指定多個初始目標To specify more than one initial target

  • Project 項目的 InitialTargets 屬性中,列出以分號分隔的初始目標。List the initial targets, separated by semicolons, in the InitialTargets attribute of the Project element. 例如,若要依序執行 Clean 目標和 Compile 目標,請輸入:For example, to run the Clean target and then the Compile target, type:

    <Project InitialTargets="Clean;Compile">

使用 DefaultTargets 屬性Use the DefaultTargets attribute

如果未在命令列上明確指定目標,則 Project 項目的 DefaultTargets 屬性會指定要建置哪些目標。The DefaultTargets attribute of the Project element specifies which target or targets are built if a target is not specified explicitly on the command line. 如果已在 InitialTargetsDefaultTargets 屬性上指定目標,且未在命令列上指定目標,則 MSBuildMSBuild 會執行 InitialTargets 屬性中指定的目標,接著執行 DefaultTargets 屬性中指定的目標。If targets are specified in both the InitialTargets and DefaultTargets attributes and no target is specified on the command line, MSBuildMSBuild runs the targets specified in the InitialTargets attribute followed by the targets specified in the DefaultTargets attribute.

指定一個預設目標To specify one default target

  • Project 項目的 DefaultTargets 屬性中,指定預設的目標。Specify the default target in the DefaultTargets attribute of the Project element. 例如:For example:

    <Project DefaultTargets="Compile">

    您可以在 DefaultTargets 屬性中指定多個預設目標,方法是依序列出目標,然後使用分號來分隔每個目標。You can specify more than one default target in the DefaultTargets attribute by listing the targets in order, and using a semicolon to separate each target. 清單中的目標將會循序執行。The targets in the list will be run sequentially.

指定多個預設目標To specify more than one default target

  • Project 項目的 DefaultTargets 屬性中,列出以分號分隔的預設目標。List the default targets, separated by semicolons, in the DefaultTargets attribute of the Project element. 例如,若要依序執行 Clean 目標和 Compile 目標,請輸入:For example, to run the Clean target and then the Compile target, type:

    <Project DefaultTargets="Clean;Compile">

使用 -target 參數Use the -target Switch

如果專案檔中未定義預設目標,或者,如果您不想使用預設目標,您可以使用命令列參數 -target 來指定不同的目標。If a default target is not defined in the project file, or if you do not want to use that default target, you can use the command line switch -target to specify a different target. 使用 -target 參數指定的目標即會執行,而不是執行 DefaultTargets 屬性所指定的目標。The target or targets specified with the -target switch are run instead of the targets specified by the DefaultTargets attribute. InitialTargets 屬性中執行的目標永遠會先執行。Targets specified in the InitialTargets attribute always run first.

優先使用非預設的目標To use a target other than the default target first

  • 使用 -target 命令列參數,將目標指定為第一個目標。Specify the target as the first target using the -target command line switch. 例如:For example:

    msbuild file.proj -target:Clean

優先使用預設目標以外的數個目標To use several targets other than the default targets first

  • 使用 -target 命令列參數,列出以分號或逗號分隔的目標。List the targets, separated by semicolons or commas, using the -target command line switch. 例如:For example:

    msbuild <file name>.proj -t:Clean;Compile

另請參閱See also

MSBuildMSBuild