为什么 MSBuild ItemGroup 条件不能在全局范围内工作 [英] Why doesn't MSBuild ItemGroup conditional work in a global scope

查看:19
本文介绍了为什么 MSBuild ItemGroup 条件不能在全局范围内工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常好奇为什么我无法根据在目标内按预期工作的元数据条件在全局范围内创建项目.例如,这按预期工作:

I'm desperately curious why I am unable to create an item in a global scope based on a metadata condition which works as expected inside a target. For instance, this works as expected:

<ItemGroup>
    <TestItems Include="TestItem1">
        <TestFlag>true</TestFlag>
    </TestItems>
    <TestItems Include="TestItem2">
        <TestFlag>false</TestFlag>
    </TestItems>
</ItemGroup>

<Target Name="Default">

    <Message Text="@(TestItems)" />
    <Message Text="@(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />

    <ItemGroup>
        <FilteredTestItems Include="@(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
    </ItemGroup>

    <Message Text="@(FilteredTestItems)" />
    <Message Text="@(FilteredTestItems)" Condition="'%(FilteredTestItems.TestFlag)'=='true'" />

</Target>

并产生以下输出:

TestItem1;TestItem2测试项目1测试项目1测试项目1

TestItem1;TestItem2 TestItem1 TestItem1 TestItem1

这按预期工作:

<ItemGroup>
    <TestItems Include="TestItem1">
        <TestFlag>true</TestFlag>
    </TestItems>
    <TestItems Include="TestItem2">
        <TestFlag>false</TestFlag>
    </TestItems>
</ItemGroup>

<ItemGroup>
    <FilteredTestItems Include="@(TestItems)" Condition="'false'=='true'" />
</ItemGroup>

<Target Name="Default">

    <Message Text="@(TestItems)" />
    <Message Text="@(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />


    <Message Text="@(FilteredTestItems)" />
    <Message Text="@(FilteredTestItems)" Condition="'%(FilteredTestItems.TestFlag)'=='true'" />

</Target>

产生以下输出:

TestItem1;TestItem2测试项目1

TestItem1;TestItem2 TestItem1

但是这个:

<ItemGroup>
    <TestItems Include="TestItem1">
        <TestFlag>true</TestFlag>
    </TestItems>
    <TestItems Include="TestItem2">
        <TestFlag>false</TestFlag>
    </TestItems>
</ItemGroup>

<ItemGroup>
    <FilteredTestItems Include="@(TestItems)" Condition="'%(TestItems.TestFlag)'=='true'" />
</ItemGroup>

产生以下 MSBuild 错误:

Produces the following MSBuild error:

temp.proj(13,45):错误 MSB4090:在条件'%(TestItems.TestFlag)'=='true'"的位置 2 处发现意外字符%".

temp.proj(13,45): error MSB4090: Found an unexpected character '%' at position 2 in condition "'%(TestItems.TestFlag)'=='true'".

那是什么?当然我可以解决它,但我对 ItemGroup、元数据和/或全局范围究竟有什么不了解?

So what gives? Certainly I can work around it, but what exactly am I not understanding about ItemGroup, metadata and/or the global scope?

推荐答案

项目组条件在目标之外有效,但批处理不起作用(即%"运算符).调用任务时会使用批处理,由于只能从目标内部调用任务,因此批处理也只能在目标内部工作是有意义的.

The item group condition works outside a target, but batching doesn't (that's the "%" operator). Batching is used when you call a task, and since you can only call a task from inside a target, it makes sense for batching to also only work inside a target.

您可能会问为什么项目组在目标中起作用,因为它不是任务.在 MSBuild 3.5 之前,您根本不允许在目标中使用项目组;您必须改为调用 CreateItem .在 3.5 和 4.0 版本中,允许以这种方式使用项目组,但我认为这只是调用 CreateItem 任务的语法糖,因此您的条件有效,因为有 is 任务在幕后.

You might ask why the item group works inside the target since it's not a task. Prior to MSBuild 3.5, you weren't allowed item groups inside targets at all; you had to call CreateItem instead. In versions 3.5 and 4.0, using item groups that way is allowed, but I think it's just syntactic sugar for calling the CreateItem task, so your condition works because there is a task behind the scenes.

这篇关于为什么 MSBuild ItemGroup 条件不能在全局范围内工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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