您的位置:首页 > 产品设计 > UI/UE

MSBuild 中的 PropertyGroup、ItemGroup 和 ItemMetadata

2014-04-22 17:35 543 查看
在软件项目不断的进展中,MSBuild 脚本可能几个月都不会被修改,因为通常编译和发布的目录是不经常变化的。

但,一旦某天你需要修改了,看到那一堆 $(Something)、 @(Something)、%(Something) 是相当的头大,不得不搜索 MSDN 才能找到合理的用法。

每次看到下面这样的语法,我都感觉,有必要把语法设计成这样吗?

<Copy SourceFiles="@(SrcFiles)" DestinationFiles="@(SrcFiles->'c:\DestDir\%(RecursiveDir)%(Filename)%(Extension)')" />


这篇文章将对 PropertyGroup、ItemGroup 和 ItemMetadata 做简单的语法介绍,解救未来的自己。

PropertyGroup 和 $ 符号

PropertyGroup 用于标记一个或多个变量值。ProperyGroup 内的 XML 节点属性(Property)可以用任何字符串命名。

属性可以通过 $(OtherPropertyName) 语法来引用其他属性的的值。同样,在其他位置使用相同方式引用属性值。

<PropertyGroup>
<BaseFolder>C:\my\folder</BaseFolder>
<SettingsFile>$(BaseFolder)\settings\app.xml</SettingsFile>
</PropertyGroup>
<Message Text="Using settings file found at $(SettingsFile)"/>


上面的 Message 命令的输出结果为:

Using settings file found at C:\my\folder\settings\app.xml


ItemGroup 和 @ 符号

ItemGroup 用于标记一个包含多个值的变量,类似于 C# 中的 Array 或 Dictionary 等。

<ItemGroup>
<MyItems Include="First" />
<MyItems Include="Second;Third;" />
<MyItems Include=";;;;Fourth;;" />
</ItemGroup>
<Message Text="My items using dollar: $(MyItems)"/>
<Message Text="My items using at symbol: @(MyItems)"/>


上面的命令输出的结果为:

My items using dollar:
My items using at symbol: First;Second;Third;Fourth


我们看到,如果使用 $ 符号只能得到一个空字符串。而使用 @ 符号则将输出以 ";" 分号分割的字符串。

同时,MSBuild 也帮我们过滤了多余的 ";" 字符。

ItemMetadata 和 % 符号

ItemGroup 不但可以被用于列表数据,还可以用于 key/value 字典。

在 MSBuild 中 key/value 被称为 ItemMetadata。

<ItemGroup>
<People Include="Joe">
<Email>joe@example.com</Email>
</People>
<People Include="Bill">
<Email>bill@example.com</Email>
</People>
<People Include="Oscar">
<Email>oscar@example.com</Email>
</People>
</ItemGroup>
<Message Text="Processing person %(People.Identity) with email %(People.Email)"/>


上面的命令输出的结果为:

Processing person Joe with email joe@example.com
Processing person Bill with email bill@example.com
Processing person Oscar with email oscar@example.com


在 %(ItemGroup.MetadataKey) 语法中,"Identity" 代表着 XML 节点中的 "Include" 属性中的值。

同时,我们发现,虽然只写了一句 Message 命令,但是有 3 条输出。这是利用的 MSBuild 中的 Task Batching 功能。

那还有哪些 Item Metadata Key 呢?参考这里 :MSBuild Well-known Item Metadata

参考资料

MSBuild Overview

MSBuild Batching

MSBuild Items

MSBuild Tasks

MSBuild Targets

MSBuild Well-known Item Metadata

Item Metadata in Task Batching

Microsoft.Build.Tasks Namespace

TaskExtension Class

How to: Specify Which Target to Build First
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐