第15讲 More about Tasks 更多关于任务.pdf

  1. 1、本文档共16页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第15章 More about Tasks 更多关于任务 在⼊门教程中 (Chapter 06. Build Script Basics 构建脚本的基础识),已经学到了如何 创建简单 task 。之后您 学习了如何将其他⾏为添加到这些 task 中,同时你已经学会 了如何创建 task 之间的依赖。这都是简单的 task 。但 Gradle 让 task 的概念更深远。 Gradle ⽀持增强的task ,也就是,有⾃⼰的属性和⽅法的 task 。这是真正的与你所使 ⽤的 Ant target (⽬标)的不同之处。这种增强的任务可以由你提供,或由 Gradle 构 建。 15.1. Defining tasks 定义任务 在 (Chapter 06. Build Script Basics 构建脚本的基础识)中我们已经看到如何通过关键 字这种风格来定义 task 。在某些情况中,你可能需要使⽤这种关键字风格的⼏种不同 的变式。例如,在表达式中不能⽤这种关键字风格。 Example 15.1. Defining tasks uild .gradle task(hello) { println hello } task(copy, type: Copy) { from(file(srcDir)) into(buildDir) } 也可以使⽤字符串作为 task 名称 Example 15.2. Defining tasks - using strings for task names uild .gradle task(hello) { println hello } task(copy , type: Copy) { from(file(srcDir)) into(buildDir) } 你可能更愿意使⽤另外⼀种替代的语法来定义任务: Example 15.3. Defining tasks with alternative syntax uild .gradle tasks create(name: hello) { println hello } tasks create(name: copy , type: Copy) { from(file(srcDir)) into(buildDir) } 15.2. Locating tasks 定位任务 你经常需要在构建⽂件中查找你所定义的 task ,例如,为了去配置或是使⽤它们作为 依赖。对这样的情况,有很多种⽅法。⾸先,每个 task 都可作为 project 的⼀个属 性,并且使⽤ task 名称作为这个属性名称: Example 15.4 . Accessing tasks as properties uild .gradle task hello println hello name println project hello name task 也可以通过 task 集合来访问 Example 15.5. Accessing tasks via tasks collection uild .gradle task hello println tasks hello name println tasks[hello] name 您可以从任何 project 中,使⽤ tasks.getByPath() ⽅法获取 task 路径并且通过这个路径 来访问 task 。你可以⽤ task 名称,相对路径或者是绝对路径作为参数调⽤ getByPath() ⽅法。 Example 15.6. Accessing tasks y path uild .gradle project(:projectA ) { task hello } task hello println tasks getByPath(hello) path println tasks getByPath(:hello) path println tasks getByPath(projectA :hello) path println tasks getByPath(:projectA :hello) path 执⾏ gradle -q hello gradle -q hello :hello :hello :projectA :hello :projectA :hello 详见 TaskContai

文档评论(0)

xuefei111 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档