Robert Elliot
06/19/2025, 1:07 PMRobert Elliot
06/19/2025, 1:11 PMplugins {
base
alias(libs.plugins.idea.ext)
}
val generateSources by tasks.registering {}
idea {
idea.project.settings {
taskTriggers {
afterSync(generateSources)
}
}
}
`buildlogic.kotlin-common-conventions.gradle.kts`:
val generateSources by tasks.registering {}
rootProject.tasks.findByName("generateSources")?.dependsOn(generateSources)
tasks.classes {
dependsOn(generateSources)
}
Sub project `build.gradle.kts`:
plugins {
id("buildlogic.kotlin-common-conventions")
}
val templateKotlin by tasks.registering(Sync::class) {
// generation stuff
}
tasks.generateSources { dependsOn(templateKotlin) }
Robert Elliot
06/19/2025, 1:12 PMtasks.generateSources { dependsOn(newTask) }
.Robert Elliot
06/19/2025, 1:15 PMafterSync(taskName)
call seems to need it - I can do ./gradlew generateSources
, but afterSync("generateSources")
throws an NPE if the task does not exist explicitly on the root project.)Robert Elliot
06/19/2025, 1:26 PMtasks.classes {
dependsOn(generateSources)
}
may be unnecessary, it isn't needed by the idea afterSync
.Vampire
06/20/2025, 2:42 AMfindByName
you break task configuration avoidance and it also only works if that task is registered already
• what I do is to have a generate
task (like your generateSources
, but could also depend on resource generation and so on) in each and every project, no matter whether is has actual generation or not and then use val generate by tasks.registering { dependsOn(project.subprojects.map { "${it.path}:generate" }) }
in the root project.
• As you already said, making classes
depend on the source generation does not make much sense to me, and also is the wrong semantic. That lifecycle task should depend on tasks that produce class files, not source files. The source generation tasks should be registered as source dirs for the respective source sets. By that the source generation is automatically run when the sources are needed. This includes the compilation tasks, which then already makes classes
implicitly depend on the source generation.Robert Elliot
06/20/2025, 8:24 AMval generate by tasks.registering {
dependsOn(project.subprojects.map { it.tasks.named("generateSources") })
}
Vampire
06/20/2025, 8:35 AM