1. 程式人生 > >Gradle基礎:4:Task的使用方式

Gradle基礎:4:Task的使用方式

在這裡插入圖片描述
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個任務:

建立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

  @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的執行順序:

  • 執行結果確認
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確認

將任務進行了不同的分組,更貼近實際使用的狀況:

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$ 

執行確認

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/