如何使任务依赖于 build.gradle 本身?
How do I make a Task depend on the build.gradle itself?
我有以下 build.gradle 文件。当前,任务 generateSources
每次执行 gradle 时都会运行(“构建成功”)。相反,我希望它仅在 build.gradle 文件本身更改时执行,以便构建是增量构建(“UP-TO-DATE”)
即我希望它的“输入”是“build.gradle”本身。
我该怎么做?
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
// inputs = ????
// onlyIf ???
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
(上面的代码被简化到最低限度。实际上任务生成了一些文件,并且它们在 Task.output 中正确配置)
据我所知有两种方式,finalizedBy和dependsOn
finalizedBy 意味着在此任务之后执行此操作
tasks.named("build") { finalizedBy("myTaskName") }
而 dependsOn 意味着在构建之后执行此任务
tasks.named("myTaskName") { dependsOn("build") }
上任务的更多信息
事实证明,您可以在项目文件对象本身上放置一个 input-dependency,例如 inputs.file project.buildFile
。
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
inputs.file project.buildFile // only rebuild when _this_ build.gradle changes
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
我有以下 build.gradle 文件。当前,任务 generateSources
每次执行 gradle 时都会运行(“构建成功”)。相反,我希望它仅在 build.gradle 文件本身更改时执行,以便构建是增量构建(“UP-TO-DATE”)
即我希望它的“输入”是“build.gradle”本身。
我该怎么做?
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
// inputs = ????
// onlyIf ???
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources
(上面的代码被简化到最低限度。实际上任务生成了一些文件,并且它们在 Task.output 中正确配置)
据我所知有两种方式,finalizedBy和dependsOn finalizedBy 意味着在此任务之后执行此操作
tasks.named("build") { finalizedBy("myTaskName") }
而 dependsOn 意味着在构建之后执行此任务
tasks.named("myTaskName") { dependsOn("build") }
上任务的更多信息
事实证明,您可以在项目文件对象本身上放置一个 input-dependency,例如 inputs.file project.buildFile
。
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'Main'
version = "1.0"
task generateSources() {
inputs.file project.buildFile // only rebuild when _this_ build.gradle changes
outputs.upToDateWhen { true } // in the real code this is a file
doFirst {
println("Hello, World! $project.version")
}
}
compileJava.dependsOn generateSources