Gradle实践指南:Task的使用方式

2018-12-29 10:35:34 浏览数 (1)

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

常见的属性和方法

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

示例介绍

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

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

创建task

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

方式1: task task名称 {}

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

代码语言:javascript复制
task compile {
  group 'compile'
  description 'compile task'
  println "[phase:configuration] compile"
  doFirst {
    println "[phase:execution] compile :doFirst()"
  }
}
方式2: tasks.create(name: ‘task名称’) {}

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

代码语言:javascript复制
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了,以后初入者就可以对这个语法不再纠结了。

代码语言:javascript复制
task packaging << {
  group 'packaging'
  description 'packaging task'
  println "[phase:execution] in << closure"
}
方式4: 继承DefaultTask

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

代码语言:javascript复制
class Install extends DefaultTask{
  String installObjectName

  @TaskAction
  void checkObject() {
    println "[phase:execution] install:checkObject   (${installObjectName})"
  }

  @TaskAction
  void 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 执行结果确认
代码语言:javascript复制
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_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 

结果确认

task确认

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

代码语言:javascript复制
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 task

Help 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 task

Test tasks
----------
test - test task

To see all tasks and more detail, run gradle tasks --all

To 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_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liumiaocn:hello liumiao$ 
执行确认
代码语言:javascript复制
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_warnings

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed
liumiaocn:hello liumiao$ 

参考内容

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

作者:liumiaocn 原文:https://blog.csdn.net/liumiaocn/article/details/84230057


代码人生,一飞冲天。

END

0 人点赞