代码拉取完成,页面将自动刷新
import org.gradle.work.ChangeType
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
/**
* 看效果在这 app > build.gradle > apply from : '../task.gradle'
*/
tasks.register("yechaoa") {
it.configure {
description = "我是yechaoa,双击执行~"
group = "help"
}
it.doFirst {
println("${it.name} = doFirst 111")
}
it.doFirst {
println("${it.name} = doFirst 222")
}
println("Task Name = " + it.name)
it.doLast {
println("${it.name} = doLast 111")
}
it.doLast {
println("${it.name} = doLast 222")
}
}
/**
* 自定义Task
*/
class YechaoaTask extends DefaultTask {
@Internal
def taskName = "default"
@TaskAction
def MyAction1() {
println("$taskName -- MyAction1")
}
@TaskAction
def MyAction2() {
println("$taskName -- MyAction2")
}
}
//tasks.register("yechaoa", YechaoaTask) {
// taskName = "我是传入的Task Name "
//}
/**
* Task 依赖
*/
def yechaoa111 = tasks.register("yechaoa111") {
it.doLast {
println("${it.name}")
}
}
def yechaoa222 = tasks.register("yechaoa222") {
it.doLast {
println("${it.name}")
}
}
yechaoa111.configure {
dependsOn yechaoa222
}
/**
* onlyIf
*/
//tasks.register("skipTask") { taskObj ->
// taskObj.configure {
// onlyIf {
// def provider = providers.gradleProperty("yechaoa")
// provider.present
// }
// }
//
// taskObj.doLast {
// println("${it.name} is Executed")
// }
//}
/**
* StopExecutionException
*/
//tasks.register("skipTask") { taskObj ->
//
// taskObj.doFirst {
// println("${it.name} is Executed doFirst")
// }
//
// taskObj.doLast {
// def provider = providers.gradleProperty("yechaoa")
// if (provider.present) {
// throw new StopExecutionException()
// }
//
// println("${it.name} is Executed doLast")
// }
//}
/**
* enabled
*/
//tasks.register("skipTask") { taskObj ->
// taskObj.configure {
// enabled = true
// }
//
// taskObj.doLast {
// println("${it.name} is Executed")
// }
//}
/**
* timeout
*/
//tasks.register("skipTask") { taskObj ->
// taskObj.configure {
// timeout = Duration.ofSeconds(10)
// }
//
// taskObj.doLast {
// Thread.sleep(11 * 1000)
// println("${it.name} is Executed")
// }
//}
/**
* 增量构建
*/
class CopyTask extends DefaultTask {
// 指定增量输入属性
@Incremental
// 指定输入
@InputFiles
FileCollection from
// 指定输出
@OutputDirectory
Directory to
// // task action 执行
// @TaskAction
// void execute() {
// File file = from.getSingleFile()
// if (file.isDirectory()) {
// from.getAsFileTree().each {
// copyFileToDir(it, to)
// }
// } else {
// copyFileToDir(from, to)
// }
// }
// task action 执行
@TaskAction
void execute(InputChanges inputChanges) {
boolean incremental = inputChanges.incremental
println("isIncremental = $incremental")
inputChanges.getFileChanges(from).each {
if (it.fileType != FileType.DIRECTORY) {
ChangeType changeType = it.changeType
String fileName = it.file.name
println("ChangeType = $changeType , ChangeFile = $fileName")
if (changeType != ChangeType.REMOVED) {
copyFileToDir(it.file, to)
}
}
}
}
/**
* 复制文件到文件夹
* @param src 要复制的文件
* @param dir 接收的文件夹
* @return
*/
static def copyFileToDir(File file, Directory dir) {
File dest = new File("${dir.getAsFile().path}/${file.name}")
if (!dest.exists()) {
dest.createNewFile()
}
dest.withOutputStream {
it.write(new FileInputStream(file).getBytes())
}
}
}
tasks.register("CopyTask", CopyTask) {
from = files('from')
to = layout.projectDirectory.dir('to')
}
/**
* 查找Task
*/
def aaa = tasks.findByName("yechaoa").doFirst {
println("yechaoa excuted doFirst by findByName")
}
def bbb = tasks.findByPath("yechaoa").doFirst {
println("yechaoa excuted doFirst by findByPath")
}
tasks.withType(DefaultTask).configureEach(task -> {
if (task.name.toLowerCase().contains("copytask")) {
println(task.class.name)
}
})
tasks.configureEach(task -> {
// do something
})
tasks.each {
// do something
}
tasks.forEach(task -> {
// do something
})
tasks.named("yechaoa") {
it.doFirst {
println("yechaoa excuted doFirst by named")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。