Task是Gradle的基础单元,这篇文章总结和整理一下关于task的常见的使用方式。

常见的属性和方法

在前面的例子中,我们已经使用task的description属性进行设定gradle task中的显示信息,将task进行分组显示,同时使用了doFirst和doLast方法进行回调。除了这些之外,还有一些其他的属性和方法,简单整理如下:

属性/方法 说明
name task的名称
description task的描述
group 设定任务的逻辑分组
enabled 设定任务enable或者是disable
dependson 设定dependencies的配置
doFirst 在task的开始的回调方法
doLast 在task的结束的回调方法
onlyIf 条件执行

示例介绍

这篇文章会通过对上文的例子进行重新改写,来介绍gradle对于task操作的常见方式,还是如下4个任务:

编译: compile
测试:test
打包:packaging
安装:install

创建task

gradle非常灵活,在task创建上都有很多的方式,这里列举出常见的几种,明白这几种,然后再看大部分gradle的介绍文档时就会较为轻松了。

方式1: task task名称 {}

在前面的文章中主要使用这种方式进行演示,比如:

task compile {group 'compile'description 'compile task'println "[phase:configuration] compile"doFirst {println "[phase:execution] compile :doFirst()"}
}

方式2: tasks.create(name: ‘task名称’) {}

比如,将前文中的test段的例子进行修改:

tasks.create(name: 'test') {group 'test'description 'test task'println "[phase:configuration] test"doLast {println "[phase:execution] test:doLast()"}
}

当然这种方式也可以简写称tasks.create(‘test’),这里就不再赘述。

方式3: task task名称 << {}

关于<<的解释有很多,简单来说,<<就是doLast的快捷方式。所以它会在execution阶段被执行。不过在本月底即将全面推出的Gradle 5中,这种写法已经deprecated了,以后初入者就可以对这个语法不再纠结了。

task packaging << {group 'packaging'description 'packaging task'println "[phase:execution] in << closure"
}

方式4: 继承DefaultTask

在前面的文章中介绍gradle特性的时候提到过groovy在gradle中的作用,虽然之前的例子一再展示,但是这种方式会让熟悉groovy或者java的开发者更加适应。
这里我们把前文中的install的task用这种方式进行改写一下:

class Install extends DefaultTask{String installObjectName@TaskActionvoid checkObject() {println "[phase:execution] install:checkObject   (${installObjectName})"}@TaskActionvoid installObject() {println "[phase:execution] install:installObject (${installObjectName})"}
}task install(type: Install) {group 'install'description 'install task'installObjectName 'test.jar'println "[phase:configuration] install"doFirst {println "[phase:execution] install:doFirst()"}doLast {println "[phase:execution] install:doLast()"}
}

另外,熟悉kotlin的也可以使用kotlin的方式来进行改写,这里就不再展开。相较于前面的几种方式,这种方式略显复杂,简单说明如下:

  • groovy或者kotlin作为gradle的DSL,继承诸如DefaultTask可以直接进行扩展。
  • 除了DefaultTask之外虽然也有其他的方式可以对task进行扩展,直接继承DefaultTask可能是最为常见的一种方式。
  • 创建任务时通过type建立扩展类和任务之间的关联
  • 通过installObjectName将数据传入task的执行阶段,主要用于需要进行信息交互时,这也是常见的使用场景。
  • TaskAction注解为缺省的任务活动,当有多个时会顺次执行

这个例子中请结合注意doFirst和doLast的执行顺序:

doFirst
checkObject
installObject
doLast
  • 执行结果确认
liumiaocn:hello liumiao$ gradle install
[Phase: initialization] : settings executed... > Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] install> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:checkObject   (test.jar)
[phase:execution] install:installObject (test.jar)
[phase:execution] install:doLast()Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$

结果确认

task确认

将任务进行了不同的分组,更贴近实际使用的状况:

liumiaocn:hello liumiao$ gradle tasks
[Phase: initialization] : settings executed... > Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] install> Task :tasks------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.Compile tasks
-------------
compile - compile taskHelp tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'helloPorject'.
components - Displays the components produced by root project 'helloPorject'. [incubating]
dependencies - Displays all dependencies declared in root project 'helloPorject'.
dependencyInsight - Displays the insight into a specific dependency in root project 'helloPorject'.
dependentComponents - Displays the dependent components of components in root project 'helloPorject'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'helloPorject'. [incubating]
projects - Displays the sub-projects of root project 'helloPorject'.
properties - Displays the properties of root project 'helloPorject'.
tasks - Displays the tasks runnable from root project 'helloPorject'.Install tasks
-------------
install - install taskTest tasks
----------
test - test taskTo see all tasks and more detail, run gradle tasks --allTo see more detail about a task, run gradle help --task <task>Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$

执行确认

liumiaocn:hello liumiao$ gradle compile test packaging install
[Phase: initialization] : settings executed... > Configure project :
[phase:configuration] build.gradle ...
[phase:configuration] compile
[phase:configuration] test
[phase:configuration] install> Task :compile
[phase:execution] compile :doFirst()> Task :test
[phase:execution] test:doLast()> Task :packaging
[phase:execution] in << closure> Task :install
[phase:execution] install:doFirst()
[phase:execution] install:checkObject   (test.jar)
[phase:execution] install:installObject (test.jar)
[phase:execution] install:doLast()Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
liumiaocn:hello liumiao$

参考内容

https://guides.gradle.org/writing-gradle-tasks/

Gradle基础:4:Task的使用方式相关推荐

  1. Gradle基础:2:Gradle的HelloWorld

    这篇文章使用最简单的HelloWorld例子对Gradle的使用进行概要性的说明. 事前准备 事前安装和设定好gradle liumiaocn:hello liumiao$ gradle --vers ...

  2. 深入理解gradle中的task

    文章目录 简介 定义task tasks 集合类 Task 之间的依赖 定义task之间的顺序 给task一些描述 task的条件执行 task rule Finalizer tasks 总结 简介 ...

  3. [Unity安卓开发]Unity3D Gradle基础

    Unity 将 Gradle 用于所有 Android 构建.可以在 Unity 中构建输出包(.apk..aab),也可以从 Unity 导出 Gradle 项目,然后通过外部工具(如 Androi ...

  4. 史上最详细的Android Studio系列教程四--Gradle基础

    史上最详细的Android Studio系列教程四--Gradle基础 转载于:https://www.cnblogs.com/zhujiabin/p/5125917.html

  5. 【转】Android Studio安装配置学习教程指南 Gradle基础--不错

    原文网址:http://www.linuxidc.com/Linux/2015-02/113890p4.htm 其实很早之前也写了一篇Gradle的基础博客,但是时间很久了,现在Gradle已经更新了 ...

  6. java中什么是task_关于java:深入理解gradle中的task

    简介 在之前的文章中,咱们讲到了如何应用gradle创立一个简略的task,以及task之间怎么依赖,甚至应用了程序来创立task.在本文中,咱们会更加深刻的去理解一下gradle中的task. 定义 ...

  7. Dubbo(二):Dubbo 基础配置Xml、注解方式 和 高级特性

    Dubbo的基础配置 Xml方式 注解方式 Dubbo的基础配置使用 启动时检查 超时重连 集群容错 负载均衡配置 结果缓存 服务分组 多版本 只订阅/只注册 异步调用 事件通知 参数回调 本地伪装- ...

  8. Gradle 笔记_1 - Gradle 基础

    Gradle 笔记_1 - Gradle 基础 <Gradle for Android 中文版>笔记 理解 Gradle 基础 Gradle 构建的脚本–build.gradle Grad ...

  9. Gradle基础:1: 简介与安装

    这篇文章主要介绍一下Grale的主要特性以及安装方式. 什么是Gradle Gradle是一个开源的自动构建工具,在Apache Ant和Apache Maven的相关概念基础上发展而来,与Maven ...

最新文章

  1. DeepMind提出基于视觉的强化学习模型,十八般兵器对机器人不在话下
  2. 机器学习基础专题:高斯判别分析
  3. 待飞日记(第四天和第五天)
  4. win7/10 画图程序按宽度高度mm cm精确调整图片尺寸
  5. GDCM:gdcm::SurfaceReader的测试程序
  6. 朱峰谈概念设计(三):可信的设计
  7. Eclipse搭建Android开发环境(安装ADT,Android4.4.2)
  8. vue数组刷新_Vue中数组更新后,页面没有动态刷新问题
  9. OpenCV--cvThreshold() 阈值化【转载】
  10. SQLServer之深度分析Select
  11. 多角度了解科大讯飞公司之一(语音识别)
  12. linux基础命令总结-1
  13. Android Studio3.0,在原有项目中进行ndk配置
  14. stata怎么画分类图_Stata怎么画直方图或折线图-Stata教程
  15. python 大智慧自定义数据_大智慧自定义数据
  16. ant-design,解决格式化Table中的时间
  17. (八)JVM成神路之GC分区篇:G1、ZGC、ShenandoahGC高性能收集器深入剖析
  18. WebPagetest H5性能测试工具入门详解
  19. Java利用多线程编程实现一个正在旋转的地球
  20. office 2010安装

热门文章

  1. eos节点服务器_EOS柚子生态投票的骗局,你以为自己在区块恋革命,其实是在参与CX罢了...
  2. Unity学习shader笔记[一百]简单焦散Caustic效果
  3. Depin(Linux)下安装Tibco Ems 8.5
  4. 计算机基本配置实验方案,实验4 计算机配置方案.doc
  5. 深度学习中需要的矩阵计算
  6. python爬虫(十七)12306案例
  7. 天才小毒妃 第912章 坑了一大笔
  8. 在网络世界中如何才能保护好自己的安全?
  9. 卷积神经网络之前向传播算法
  10. Domain Adaptation and Adaptive Information Fusion for Object Detection on Foggy Days