Basically what @twwwt said, but more.
You should not only not mix generated and non-generated code.
You should also not share output folders among multiple tasks or up-to-date checks and caching might be disturbed.
So latest when you have mutliple code generation tasks, give each task a separate output folder, for example layout.buildDirectory.dir("generated/sources/$name/main/java")
.
Additionally it is preferrable to use implicit task dependencies over explicit task dependencies.
That way every task that needs sources automatically depends on the needed tasks and you don’t need manual task dependencies.
You can achieve that by using the task or task provider as srcDir
directly.
It is almost always a sign of antipattern if you configure paths manually except for inputs / outputs of tasks.
So the full example would be:
plugins {
id("java")
id("maven-publish")
}
repositories {
mavenCentral()
}
java {
withSourcesJar()
}
def copyTemplates = tasks.register("copyTemplates", Copy) {
from("templates")
into(layout.buildDirectory.dir("generated/sources/$name/main/java"))
}
sourceSets {
main {
java {
srcDir(copyTemplates)
}
}
}