当您导入另一个msbuild文件时,评估的顺序是什么? [英] When you import another msbuild file, what is the order of evaluation?

查看:109
本文介绍了当您导入另一个msbuild文件时,评估的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个共享的属性文件shared.properties.proj

I have a shared properties file shared.properties.proj

<Project  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <SharedAssemblySearch>$(MSBuildProjectDirectory)\..\Shared Assemblies</SharedAssemblySearch>
    <ParentDir>..</ParentDir>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblyPath Condition="Exists('$(SharedAssemblySearch)')">$(SharedAssemblySearch)</SharedAssemblyPath>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">..\SharedAssemblies</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblySearch Condition="!Exists('$(SharedAssemblySearch)')">$(ParentDir)\$(SharedAssemblySearch)</SharedAssemblySearch>
    <SharedAssemblyPath Condition="Exists('$(SharedAssemblySearch)')">$(SharedAssemblySearch)</SharedAssemblyPath>
 </PropertyGroup>
</project>

我正在搜索包含名为Shared Assemblies的目录的任何级别的父目录.或者SharedAssemblies

I am searching for whatever level parent directory contains the directory named Shared Assemblies. or alternatively SharedAssemblies

我想将此代码放在sln的中央位置,以便所有项目都可以导入它. sln中的项目并非都处于同一层次结构级别.

I'd like to put this code in a central location for the sln, so that all the projects can just import it. projects in the sln are not all at the same hierarchy level.

示例.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Shared.Properties.proj))\Shared.Properties.proj"
   Condition=" '$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), Shared.Properties.proj))' != '' "/>
    <ItemGroup>
    <Reference Include="EntityFramework">
      <HintPath>$(SharedAssemblyPath)\NuGet\EntityFramework.4.3.0\lib\net40\EntityFramework.dll</HintPath>
     </Reference>
    </ItemGroup>
  <Target Name="CheckReferencePaths" BeforeTargets="ResolveAssemblyReferences">
    <Message Importance="high" Text="Doing CheckReferencePaths" />
    <ItemGroup>
     <SharedAssemblyPathItem Include="$(SharedAssemblyPath)" />
    </ItemGroup>
    <Warning Condition="!Exists('@(SharedAssemblyPathItem)')" Text="SharedAssemblyPath not found at '@(SharedAssemblyPathItem)'" />
    <Warning Condition="!Exists('@(SharedAssemblyPathItem)')" Text="SharedAssemblyPath not found at '@(SharedAssemblyPathItem->'%(FullPath)')'" />
    <Message Condition="!Exists('%(Reference.HintPath)')" Text="FullPath=%(Reference.HintPath)" Importance="high" />

我可以在主项目中进行此工作,而无需将属性组推送到我导入的卫星文件中,但是现在希望使其可以在可能具有共享引用的其他项目之间重用.

I have this working in the main project without pushing the property group out to a satellite file that I import, but now want to make it reusable between other projects that could have shared references.

BeforeTargets目标在不起作用的新尝试中显示了此信息:

The BeforeTargets target shows this on the new attempt that is not working:

CheckReferencePaths: 做CheckReferencePaths D:\ projects \ Team \ Project \ Adapters \ DbAdapter \ dbadapter.csproj(103,5): 警告:"处找不到SharedAssemblyPath D:\ projects \ Team \ Project \ Adapters \ DbAdapter \ dbadapter.csproj(104,5): 警告:在"
"中找不到SharedAssemblyPath FullPath = \ NuGet \ EntityFramework.4.3.0 \ lib \ net40 \ EntityFramework.dll
FullPath =

CheckReferencePaths: Doing CheckReferencePaths D:\projects\Team\Project\Adapters\DbAdapter\dbadapter.csproj(103,5): warning : SharedAssemblyPath not found at '' D:\projects\Team\Project\Adapters\DbAdapter\dbadapter.csproj(104,5): warning : SharedAssemblyPath not found at ''
FullPath=\NuGet\EntityFramework.4.3.0\lib\net40\EntityFramework.dll
FullPath=

在评估项目组的提示路径之前,如何获取导入共享的项目文件以评估导入的项目的属性.还是评估顺序正确,但我的构造中有其他错误?

How can I get the project file that imports the shared to evaluate the imported project's properties before it evaluates the item groups' hintpaths. Or is the evaluation order proper, but something else in my construction is incorrect?

推荐答案

您的问题使我找到了这个有价值的 MSDN 上的信息.我将其发布在此处,用于保持答案自包含.

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

当MSBuild到达 Import 元素时,已导入的项目将有效地插入到 Import 元素所在位置的导入项目中.因此,导入元素的位置会影响属性和项目的值.了解导入项目设置的属性和项目以及导入项目使用的属性和项目很重要.

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.

在构建项目时,首先评估所有属性,然后评估项目.例如,以下XML定义了导入的项目文件MyCommon.targets:

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>

以下XML定义了MyApp.proj,它导入了MyCommon.targets:

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"

Name="MyCommon"

由于在MyApp.proj中定义了属性Name之后导入了项目,因此MyCommon.targets中Name的定义将覆盖MyApp.proj中的定义.如果在定义属性Name之前导入了项目,则构建将显示以下消息:

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"

Name="MyApp"

  1. 在项目文件中定义所有使用的属性和项目 作为导入项目中属性和项目的参数.

  1. Define, in the project file, all properties and items that are used as parameters for properties and items in the imported project.

导入项目.

在项目文件中定义所有必须包含的属性和项目 覆盖导入的属性和项目的默认定义 项目.

Define in the project file all properties and items that must override default definitions of properties and items in the imported project.

示例

下面的代码示例显示了第二个代码示例导入的MyCommon.targets文件. .targets文件评估导入项目中的属性以配置构建.

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>

下面的代码示例导入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>

这篇关于当您导入另一个msbuild文件时,评估的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆