iT邦幫忙

2021 iThome 鐵人賽

DAY 12
1
Software Development

Gradle 通靈術系列 第 12

第十二天:初探 Gradle 任務

  • 分享至 

  • twitterImage
  •  

任務(Task)是 Gradle 運行時的基本單位,基本上所有我們輸入的 Gradle 指令都是對應到一個任務上。今天我們就來探索一下 Gradle 任務及如何使用相關指令。

Gradle 任務的來源

Gradle 的架構非常彈性,可以依照專案的不同需求來做擴充,也因此每個專案可以執行的 Gradle 任務數量會不同。一個專案可使用的 Gradle 任務有三種來源:

  1. Gradle 內建的任務:每一個 Gradle 專案在出廠時就會內建的幾個基本任務,這些任務提供最常見的基本功能,不需要安裝任何 Plugin 就可以使用,如 wrapperhelpdependencies
  2. 由 Plugin 提供的任務:Gradle 內建沒有提供的任務,可以透過安裝 Plugin 來擴充。只要在 build.gradle.kts 裡新增 Plugin,就可以擁有其他開發者預先寫好的任務。比方說安裝了 application 這個 Plugin 就會增加 testrunbuild 等任務。
  3. 專案客製化的任務:Gradle 支援針對專案的個別需求進行擴充,開發者可以自行開發新的任務並掛載到 Gradle 裡,如此工作就可以交由 Gradle 重複執行。

取得專案可執行的任務列表

接續昨天的練習,我們來看一下新建立好的專案有哪些 Gradle 任務可以使用?請輸入以下指令來觀察終端機裡輸出的內容:

$ ./gradlew tasks

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle-practice'.
dependencies - Displays all dependencies declared in root project 'gradle-practice'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle-practice'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
outgoingVariants - Displays the outgoing variants of root project 'gradle-practice'.
projects - Displays the sub-projects of root project 'gradle-practice'.
properties - Displays the properties of root project 'gradle-practice'.
tasks - Displays the tasks runnable from root project 'gradle-practice' (some of the displayed tasks may belong to subprojects).

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

您應該會看到 Gradle 列出了很大一片的任務清單,每一個任務都可以透過一個對應的指令呼叫。換句話說,只要在 ./gradlew 後面接上任務的指令名稱就可以觸發該動作。另外您也會發現,這些任務是以「群組」分類的,方便我們了解功能屬性及記憶。

在這邊注意一下,因為我們在建立專案的時候,Gradle 已經自動幫我們安裝了 Gradle Wrapper,所以接下來要下 Gradle 指令時,都一併改用 Gradle Wrapper 提供的 gradlewgradlew.bat 喔!

取得完整任務清單

tasks 指令預設不會顯示沒有被歸類到群組的任務,假如想要查詢完成的任務清單,請在執行 tasks 指令時加上 --all 參數:

$ ./gradlew tasks --all

跟上一步的指令比對一下,會發現像 compileJavaprocessResources 剛剛都沒出現在清單裡。

不清楚任務的行為?

您會發現什麼程式碼都還沒寫就已經一堆指令可以執行,但對這些指令背後在執行什麼任務是一點頭緒都沒有。這時我們可以用 $ ./gradlew help --task <task> 指令來查詢任務說明:

$ ./gradlew help --task run
> Task :help
Detailed task information for run

Path
     :app:run

Type
     JavaExec (org.gradle.api.tasks.JavaExec)

Options
     --args     Command line arguments passed to the main class.

     --debug-jvm     Enable debugging for the process. The process is started suspended and listening on port 5005.

Description
     Runs this project as a JVM application

Group
     application

run 這個任務為例,Gradle 會把該任務的 Path、Type、Options、Description、Group 都列出來,看完這個說明後,應該就對這個指令更熟悉了吧?

執行子專案的任務

在使用 $ gradlew tasks --all 指令時,是不是有發現很多指令前面都多了 app: 的前綴字呢?Gradle 支援子專案架構,以我們的練習專案為例,app 資料夾是根目錄底下的一個子專案,透過 settings.gradle.kts 裡的 include() 函式載入進來的。所以在任務清單前的 app: 代表的是從 app 這個子專案提供的任務的意思。

相依任務

Gradle 的行為其實是由一系列任務所組成的,也因為有這種類似「堆積木」或「串流水線」的設計,才能根據各種需求組合出彈性的建置行為。而為了完成一個複雜的建置任務,各個任務之間也會產生出相依性,這種彼此串連的關係,就是 Gradle 執行任務時的 Directed Acyclic Graphs (DAGs)。所以在設計建置流程時,別忘了利用這種特性,把多個小任務串連起來,組合出完整的建置流程會更好開發也更好維護喔!

小結

今天我們了解了 Gradle 任務的組成、運作方式以及如何取得完整清單和指令說明,也試著執行了幾個 Gradle 指令,希望大家能對 Gradle 任務有更實際的體驗。明天我們將來看一下 Gradle Build Script 的基本架構。


上一篇
第十一天:學習 Gradle 的第一個指令 - init
下一篇
第十三天:初探 Gradle Build Script
系列文
Gradle 通靈術24
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言