MSBuild 中的 PropertyGroup、ItemGroup 和 ItemMetadata
2014-04-22 17:35
543 查看
在软件项目不断的进展中,MSBuild 脚本可能几个月都不会被修改,因为通常编译和发布的目录是不经常变化的。
但,一旦某天你需要修改了,看到那一堆 $(Something)、 @(Something)、%(Something) 是相当的头大,不得不搜索 MSDN 才能找到合理的用法。
每次看到下面这样的语法,我都感觉,有必要把语法设计成这样吗?
这篇文章将对 PropertyGroup、ItemGroup 和 ItemMetadata 做简单的语法介绍,解救未来的自己。
属性可以通过 $(OtherPropertyName) 语法来引用其他属性的的值。同样,在其他位置使用相同方式引用属性值。
上面的 Message 命令的输出结果为:
上面的命令输出的结果为:
我们看到,如果使用 $ 符号只能得到一个空字符串。而使用 @ 符号则将输出以 ";" 分号分割的字符串。
同时,MSBuild 也帮我们过滤了多余的 ";" 字符。
在 MSBuild 中 key/value 被称为 ItemMetadata。
上面的命令输出的结果为:
在 %(ItemGroup.MetadataKey) 语法中,"Identity" 代表着 XML 节点中的 "Include" 属性中的值。
同时,我们发现,虽然只写了一句 Message 命令,但是有 3 条输出。这是利用的 MSBuild 中的 Task Batching 功能。
那还有哪些 Item Metadata Key 呢?参考这里 :MSBuild Well-known Item Metadata
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
但,一旦某天你需要修改了,看到那一堆 $(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 OverviewMSBuild 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
相关文章推荐
- MSBuild 中的 PropertyGroup、ItemGroup 和 ItemMetadata
- [windpole]用于WF的InfoPath表单的ItemMetaData辅助数据源内的“字段”的作用
- Populate A List Item With Record Group In Oracle Forms Using Populate_List And Create_Group_From_Query Command
- 解决listview的item为自定义viewgroup时,viewgroup子view press时,整个viewgroup都被按下的问题
- 在动态添加Footer SummaryItem和Group的Item
- QGroupBox添加QCheckBox和QLineEdit限制只能整数输入,QComboBox添加选项Item,QLineEdit密码形式.QWidget添加背景,
- 解决ListView的item含有RadioGroup,滑动错乱问题,从国外大神博借鉴过来的
- 关于RadioGroup某个item设置为check后,会一直选中的问题
- org.tinygroup.metadata-元数据定义
- Android中ExpandableListView中含有Checkbox,Group长按事件无效,ChildItem长按事件有效
- 安卓ExpandableListView长按监听判断点击的item属于哪个group(群组)和child(子项)
- ListView GridView 自定义ViewGroup的单个item 刷新 和 单个item中的局部元素刷新
- 用LayoutAnimationController给ViewGroup的item添加动画,以GridView为例
- CEPH RGW集群和bucket的zone group 不一致导致的404异常解决 及 使用radosgw-admin metadata 命令设置bucket metadata 的方法
- ExpandableListView长按GroupItem时,如何处理
- Populating Tree Item With Record Group In Oracle Forms
- 带你实现ViewGroup规定行数、item居中的流式布局
- 求助 ExpandableListView group的item有间距child间隔不变
- 答网友疑问:如何动态设置ItemControl的GroupStyle