通过在构建脚本中创建一个简单的任务,了解创作 Gradle 任务的基础知识。
第 1 步:了解任务
任务是包含操作序列的可执行代码段。
doFirst{}
操作通过和闭包添加到任务中doLast{}
。
一个任务可以依赖于其他任务。
步骤 2. 注册和配置任务
在本教程的早期,我们task1
在app
构建脚本中注册和配置:
app/build.gradle.kts
tasks.register("task1"){ (1)
println("REGISTER TASK1: This is executed during the configuration phase")
}
tasks.named("task1"){ (2)
println("NAMED TASK1: This is executed during the configuration phase")
doFirst {
println("NAMED TASK1 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK1 - doLast: This is executed during the execution phase")
}
}
1 | You can use the register() method to create new tasks. |
2 | You can use the named() method to configure existing tasks. |
app/build.gradle
tasks.register("task1") { (1)
println("REGISTER TASK1: This is executed during the configuration phase")
}
tasks.named("task1") { (2)
println("NAMED TASK1: This is executed during the configuration phase")
doFirst {
println("NAMED TASK1 - doFirst: This is executed during the execution phase")
}
doLast {
println("NAMED TASK1 - doLast: This is executed during the execution phase")
}
}
1 | You can use the register() method to create new tasks. |
2 | You can use the named() method to configure existing tasks. |
步骤 3. 创建自定义任务
要创建自定义任务,您必须DefaultTask
在 Groovy DSL 或DefaultTask
Kotlin DSL 中进行子类化。
创建一个使用以下代码调用的自定义类,并将其添加到或文件LicenseTask
的底部:gradle/license-plugin/plugin/src/main/kotlin/license/LicensePlugin.kt
gradle/license-plugin/plugin/src/main/groovy/license/LicensePlugin.groovy
gradle/license-plugin/plugin/src/main/kotlin/license/LicensePlugin.kt
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.InputStream
import java.nio.charset.Charset
class LicensePlugin: Plugin<Project> {
// Don't change anything here
}
abstract class LicenseTask : DefaultTask() {
@Input
val fileName = project.rootDir.toString() + "/license.txt"
@TaskAction
fun action() {
// Read the license text
val licenseText = File(fileName).readText()
// Walk the directories looking for java files
File(project.rootDir.toString()).walk().forEach {
if (it.extension == "java") {
// Read the source code
var ins: InputStream = it.inputStream()
var content = ins.readBytes().toString(Charset.defaultCharset())
// Write the license and the source code to the file
it.writeText(licenseText + content)
}
}
}
}
gradle/license-plugin/plugin/src/main/groovy/license/LicensePlugin.groovy
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
class LicensePlugin implements Plugin<Project> {
// Don't change anything here
}
abstract class LicenseTask extends DefaultTask {
@Input
def fileName = project.rootDir.toString() + "/license.txt"
@TaskAction
void action() {
// Read the license text
def licenseText = new File(fileName).text
// Walk the directories looking for java files
new File(project.rootDir.toString()).eachFileRecurse { file ->
int lastIndexOf = file.getName().lastIndexOf('.')
if ((lastIndexOf != -1) && (file.getName().substring(lastIndexOf)) == ".java") {// Read the source code
def content = file.getText()
//println(licenseText + '\n' + content)
// Write the license and the source code to the file
file.text = licenseText + '\n' + content
}
}
}
}
该类LicenseTask
封装任务操作逻辑并声明任务期望的任何输入和输出。
任务操作用 注释@TaskAction
。在内部,逻辑首先找到一个名为“license.txt”的文件。此文件包含 Apache 许可证的文本:
许可证.txt
/*
* Licensed under the Apache License
*/
然后,该任务查找具有扩展名的文件.java
并添加许可证标头。
该任务有一个输入,即许可证文件名,以@Input
.
Gradle 使用@Input
注释来确定任务是否需要运行。如果该任务之前未运行过或者输入值自上次执行以来已更改,则 Gradle 将执行该任务。
虽然已创建自定义类,但尚未将其添加到LicensePlugin
.LicenseTask
目前无法运行。
您现在所能做的就是确保./gradlew build
运行不会失败:
$ ./gradlew build
SETTINGS FILE: This is executed during the initialization phase
> Configure project :app
BUILD SCRIPT: This is executed during the configuration phase
BUILD SUCCESSFUL in 1s
13 actionable tasks: 6 executed, 7 up-to-date
下一步: 编写插件>>