任务(Task)是 Gradle 运行时的基本单位,基本上所有我们输入的 Gradle 指令都是对应到一个任务上。今天我们就来探索一下 Gradle 任务及如何使用相关指令。
Gradle 的架构非常弹性,可以依照专案的不同需求来做扩充,也因此每个专案可以执行的 Gradle 任务数量会不同。一个专案可使用的 Gradle 任务有三种来源:
wrapper
、help
、dependencies
。build.gradle.kts
里新增 Plugin,就可以拥有其他开发者预先写好的任务。比方说安装了 application
这个 Plugin 就会增加 test
、run
、build
等任务。接续昨天的练习,我们来看一下新建立好的专案有哪些 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 提供的 gradlew
或 gradlew.bat
喔!
tasks
指令预设不会显示没有被归类到群组的任务,假如想要查询完成的任务清单,请在执行 tasks
指令时加上 --all
参数:
$ ./gradlew tasks --all
跟上一步的指令比对一下,会发现像 compileJava
、processResources
刚刚都没出现在清单里。
您会发现什麽程序码都还没写就已经一堆指令可以执行,但对这些指令背後在执行什麽任务是一点头绪都没有。这时我们可以用 $ ./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 的基本架构。
安能取熊掌而舍鱼? 便捷初始化器语法 今天听到坐在我旁边的 Ray 提到便捷初始化语法,我惊了,事实...
如今,人人都在谈人工智慧,而程序语言百百种,我们到底要学习哪一种呢?如过现在想要学习一种好入门、好上...
一样开头先上成果~ 完整影片可见: https://twitter.com/Ruinland_Mas...
AWS 资料中心基础设施:AWS Region、AZ 及 VPC 介绍 这篇将为大家介绍AWS Re...
-Stride、VAST、Trike 等:哪种威胁建模方法适合您的组织? 风险敞口是根据可能性、後...