When you import another msbuild file, what is the order of evaluation?

假装没事ソ 提交于 2019-12-05 00:31:25
KMoraz

Your question lead me to find this valuable info on MSDN. I'm posting it here for keeping the answer self-contained.


Order of Evaluation

When MSBuild reaches an Import element, the imported project is effectively inserted into the importing project at the location of the Import element. 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. For 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>

The 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"

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. 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

The following code example shows the MyCommon.targets file that the second code example imports. 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>

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>
Ludwo

Try to use inline task instead of shared.properties.proj file. Check my answer on this question.

It was about searching for some file is the parent directories. You can adapt it to search for a parent directory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!