文檔
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.