HOW TO:在多個專案檔中使用相同的目標How to: Use the same target in multiple project files

如果您撰寫了數個 MSBuildMSBuild 專案檔,您可能已發現到需要在不同的專案檔中使用相同的工作和目標。If you have authored several MSBuildMSBuild project files, you might have discovered that you need to use the same tasks and targets in different project files. 您可以不在每個專案檔中包含這些工作或目標的完整描述,而是在一個個別的專案檔中儲存目標,然後將該專案匯入到任何其他需要使用該目標的專案中。Instead of including the complete description of those tasks or targets in every project file, you can save a target in a separate project file and then import that project into any other project that needs to use the target.

使用 Import 項目Use the Import element

使用 Import 元素可將一個專案檔插入到另一個專案檔中。The Import element is used to insert one project file into another project file. 所要匯入的專案檔必須是有效的 MSBuildMSBuild 專案檔,並且包含格式正確的 XML。The project file that is being imported must be a valid MSBuildMSBuild project file and contain well-formed XML. Project 屬性會指定所匯入之專案檔的路徑。The Project attribute specifies the path to the imported project file. 如需 Import 項目的詳細資訊,請參閱 Import 項目 (MSBuild)For more information on the Import element, see Import element (MSBuild).

匯入專案To import a project

  1. 在匯入端專案檔中,定義用來作為所匯入專案中屬性和項目之參數的所有屬性和項目。Define, in the importing project file, all properties and items that are used as parameters for properties and items in the imported project.

  2. 使用 Import 元素來匯入專案。Use the Import element to import the project. 例如:For example:

    <Import Project="MyCommon.targets"/>

  3. Import 元素之後,定義必須覆寫所匯入專案中預設屬性和項目定義的所有屬性和項目。Following the Import element, define all properties and items that must override default definitions of properties and items in the imported project.

評估的順序Order of evaluation

MSBuildMSBuild 到達 Import 元素時,會將所匯入之專案有效地插入到匯入端專案中 Import 元素的位置。When MSBuildMSBuild reaches an Import element, the imported project is effectively inserted into the importing project at the location of the Import element. 因此,Import 元素的位置會影響屬性和項目的值。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. 例如,下列 XML 定義所匯入的專案檔 MyCommon.targetsFor 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>

下列 XML 定義 MyApp.proj,它會匯入 MyCommon.targetsThe 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"

由於會在 MyApp.proj 中定義 Name 屬性之後,才匯入專案;因此 MyCommon.targetsName 的定義會覆寫 MyApp.proj 中的定義。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. 如果是在定義 Name 屬性之前就匯入專案,組建就會顯示下列訊息: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

  1. 在專案檔中,定義用來作為所匯入專案中屬性和項目之參數的所有屬性和項目。Define, in the project file, all properties and items that are used as parameters for properties and items in the imported project.

  2. 匯入專案。Import the project.

  3. 在專案檔中,定義必須覆寫所匯入專案中預設屬性和項目定義的所有屬性和項目。Define in the project file all properties and items that must override default definitions of properties and items in the imported project.

範例Example

下列程式碼範例示範第二個程式碼範例匯入的 MyCommon.targets 檔案。The following code example shows the MyCommon.targets file that the second code example imports. .targets 檔案會評估來自匯入端專案的屬性以設定組建。The .targets file evaluates properties from the importing project to configure the build.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Flavor Condition="'$(Flavor)'==''">DEBUG</Flavor>
        <Optimize Condition="'$(Flavor)'=='RETAIL'">yes</Optimize>
        <appname>$(MSBuildProjectName)</appname>
    <PropertyGroup>
    <Target Name="Build">
        <Csc Sources="hello.cs"
            Optimize="$(Optimize)"
            OutputAssembly="$(appname).exe"/>
    </Target>
</Project>

範例Example

下列程式碼範例會匯入 MyCommon.targets 檔案。The following code example imports the MyCommon.targets file.

<Project DefaultTargets="Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <Flavor>RETAIL</Flavor>
    </PropertyGroup>
    <Import Project="MyCommon.targets"/>
</Project>

另請參閱See also