在软件项目不断的进展中,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

参考资料

最新文章

  1. 【NLP】揭秘马尔可夫模型神秘面纱系列文章(一)
  2. APP远程调试及网络自动化测试
  3. [C#]LDAP验证用户名和密码
  4. Java学习笔记之:java环境搭建
  5. 深度RAMOS,把操作系统全部安装在内存上
  6. 探索Oracle之数据库升级七 11gR2 to 12c 升级完毕后插入PDB
  7. UIBezierPath 和 CAShapeLayer 绘画图纸
  8. UIImage图片拉伸方法
  9. 翻译一篇文章:It&#39;s Difficult to Grow a Test Developer(成为测试开发工程师的艰辛)
  10. python可视化--matplotlib
  11. MockMvc模拟对controller进行单元测试
  12. 2.Magicodes.NET框架之路——策略管理
  13. .Net分布式锁
  14. [LeetCode&amp;Python] Problem 744. Find Smallest Letter Greater Than Target
  15. commit lint规范化团队git提交信息
  16. DotNetCore学习-3.管道中间件
  17. dp填表法,刷表法
  18. Spring基于的注解自动装配和依赖注入(***)
  19. Unity3d 插值Lerp与Slerp
  20. VS NuGet加载本地程序包

热门文章

  1. cookie,session原理,以及如何使用chrome查看。
  2. MCMC and Bayesian Data Analysis(PPT在文件模块)
  3. EF支持mysq相关配置数码
  4. 用Backbone.js创建一个联系人管理系统(三)
  5. Python 第五天 模块(2)
  6. Android视图状态及重绘流程分析,带你一步步深入了解View(三)
  7. iframe的自适应
  8. Sublime无法使用package control安装插件
  9. js读写Cookie问题(Cookie存储时长、Cookie存储域)汇总
  10. LInux配置jdk(mac和windows)