文档
https://gradle.org/guides/#getting-started
https://docs.gradle.org/current/userguide/tutorial_using_tasks.html
https://doc.yonyoucloud.com/doc/wiki/project/GradleUserGuide-Wiki/build_script_basics/task_dependencies.html
安装
- 安装SDKMAN
sudo apt update &&
sudo apt install zip unzip curl sed -y &&
curl -s "https://get.sdkman.io" | bash
按照提示执行
source "$HOME/.sdkman/bin/sdkman-init.sh"
- 安装gradle
sdk install gradle 6.6
创建Gradle项目
gradle init # 进入交互式界面
gradle init --dsl kotlin # Kotlin DSL 构建, 将生成build.gradle.kts文件, 而非build.gradle
gradle init --type java-library # 命令行指定参数,此处指定构建类型为java库
一探Kotlin DSL
由于Kotlin DSL比Groovy DSL更易懂, 我们通过它来研究Gradle
第一次使用Kotlin DSL时, Gradle会自动下载该扩展:
Generating gradle-kotlin-dsl-extensions-6.6.jar
新建一个目录mygradle, 创建文件build.gradle.kts
:
log("tasks::class.java => ", tasks::class.java)
log("tasks::class.java.name => ", tasks::class.java.name)
fun log(vararg obj: Any) {
obj.forEach {
print("$it ")
}
println()
}
执行命令gradle
, 看到内置变量tasks
是何对象:
~/mygradle$ gradle
> Configure project :
tasks::class.java => class org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
tasks::class.java.name => org.gradle.api.internal.tasks.DefaultTaskContainer_Decorated
> Task :help
Welcome to Gradle 6.6.
...
注册任务
tasks.register("hello") {
doLast {
println("Hello, Gradle!")
}
}
tasks.register("hello2", {
doLast {
println("Hello, Gradle!")
}
})
tasks.register("hello3", {
doLast({
println("Hello, Gradle!")
})
})
现在执行gradle hello -quiet
或者缩写gradle hello -q
, 我们看到打印了Hello, Gradle!
, 任务hello2和hello3也是一样, 上面三种写法是等价的.
插件
对于一个空的build.gradle.kts文件, 当我们使用gradle tasks
命令时:
$ gradle tasks
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------
Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mygradle'.
components - Displays the components produced by root project 'mygradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'mygradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mygradle'.
dependentComponents - Displays the dependent components of components in root project 'mygradle'. [incubating]
help - Displays a help message.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
model - Displays the configuration model of root project 'mygradle'. [incubating]
outgoingVariants - Displays the outgoing variants of root project 'mygradle'.
projects - Displays the sub-projects of root project 'mygradle'.
properties - Displays the properties of root project 'mygradle'.
tasks - Displays the tasks runnable from root project 'mygradle'.
To see all tasks and more detail, run gradle tasks --all
当我们添加一个java插件时:
$ cat build.gradle.kts
plugins {
java
}
$ gradle tasks
> Task :tasks
------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------
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.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'mygradle'.
components - Displays the components produced by root project 'mygradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'mygradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'mygradle'.
dependentComponents - Displays the dependent components of components in root project 'mygradle'. [incubating]
help - Displays a help message.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
model - Displays the configuration model of root project 'mygradle'. [incubating]
outgoingVariants - Displays the outgoing variants of root project 'mygradle'.
projects - Displays the sub-projects of root project 'mygradle'.
properties - Displays the properties of root project 'mygradle'.
tasks - Displays the tasks runnable from root project 'mygradle'.
Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.
Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradle tasks --all
当添加一个application
插件时,多出以下几个任务,用于分发应用程序。
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.