I am writing my first gradle plugin and currently working on dependsOn() function call availability issue in gradle 8.0.
The full code is:
class CustomPlugin: Plugin<Project> {
override fun apply(project: Project) {
project.tasks.register<HelloTask>("hello"){
dependsOn("build")
}
}
}
I use this post and gradle-recipes for reference. Not everything is up to date, but I do not have a better sources to speed up a learning curve for this tech. Migration from 7 to 8 brought many unclear dependencies failures and here is a one of them.
Could you please clarify dependsOn usage in this context? Is it still relevant here?
Thanks!
Update:
Code sample to reproduce the issue:. Take a look on HelloTask and MyPlugin. I used an existing gradle-recipe repo to avoid dealing with dependencies issues and writing my first plugin in existing sample.
The problem - besides that you do not use the function you showed in your post which just looks nicer - you use the wrong plugin in your buildSrc build script. You should not use the Kotlin/JVM plugin, especially not with a version, as it might then differ from the built-in Kotlin runtime.
Instead of the Kotlin/JVM plugin you should apply the kotlin-dsl plugin. One effect is, that you use the appropriate Kotlin version. The other effect is, that the sam-with-receiver Kotlin compiler plugin is applied.
Without it, the task to be configured within register(...) { ... } is available as it, so you need to do it.dependsOn(...). With it, it is available as this and you can just do dependsOn(...).
Above is the existing build.gradle.kts from buildSrc. Here is what I found when search for kotlin-dsl plugin. After applying it build.gradle.kts will look like one below, but syncing gives me an error:
Unable to load class 'org.gradle.kotlin.dsl.assignment.internal.KotlinDslAssignment'.
This is an unexpected error. Please file a bug containing the idea.log file.
The issue is what you have shared is not covered by publication I used for reference, neither in an official documentation or gradle publications regarding breaking changes in the v8.0 release. The only mention about this format of gradle plugin I have found here, but it suggest to use java-gradle-plugin - there is no any reference to kotlin-dsl.
kotlin-dslis documented as the plugin to use for custom build logic in Kotlin.
And it is not new to Gradle 8, it is there since the Kotlin DSL exists which is quite some years.
There was no breaking change in Gradle 8 regarding that.
Also the publication you referred to did not mention applying the Kotlin/JVM plugin from a cursory look.
It probably just went without a build script, or at least I do not see any mentioned, in which case the special buildSrc build does automatically have the kotlin-dsl plugin applied as far as I remember. I’m not sure though, as I prefer to have it exlicit and actually prefer an included build over buildSrc anyway.
And that gradle-recipes repo you linked to is just a bad reference as it uses the Kotlin/JVM plugin there which is highly discouraged and bad practice. But it also then adapts to that and uses it instead of this.
Could you please share sources besides official gradle documentaion which are good for reference from your perspective?
Currently my interests to gradle is based on Android development and Google’s code source and publications are important. If they are a bit (or a lot?) misleading, it would be good to know a better alternative.