I'm trying to write a gradle plugin, I put it in /...
# gradle
u
I'm trying to write a gradle plugin, I put it in /buildSrc, I define the META-INF/gradle-plugins/foo.bar.properties with fully qualified name, I apply it at the target project with
apply plugin: foo.bar
. Everyting works Then I change the plugin source from groovy to kotlin, and it doesn't work
Copy code
class BrowserStackPlugin :Plugin<Project> {
    override fun apply(project: Project) {
       ...
    }
}
Copy code
A problem occurred evaluating project ':app'.
> No implementation class specified for plugin 'foo.bar' in jar:file:/C:/Users/ursus/.gradle/caches/jars-8/d86af8da5bcd5c16642c4dc40605c5b9/buildSrc.jar!/META-INF/gradle-plugins/foo.bar.properties.
Are kotlin plugins not supported in buildSrc??
e
they are supported, but buildSrc/build.gradle(.kts) needs to apply a kotlin (or embedded-kotlin or kotlin-dsl) plugin
u
Hmm but I don't want the dsl so far, I only use kotlin to write the plugin with autocomplete; but I'm not familiar with the embedded one Trying putting this in build.gradle
Copy code
plugins {
    id "kotlin"
}
is probably not supposed to work right
Btw would you say a separate module for the plugin is better, if its only going to be consumed within the project, never shared outside? I see the composite build way of doing this, of which im not sure why would I need that, can't I just include a local module for the build classpath?
e
the usual org.jetbrains.kotlin.jvm plugin, or the org.gradle.embedded-kotlin one (which will be sure to match the version that's embedded in Gradle)
1
buildSrc ends up in the root buildscript classpath, while if you use a separate build, it will only be loaded in the projects that need it
u
Hm, so then my snippet should make it work, no?
m
Try to add a syntax error to your Kotlin code to make sure it's compiled.
If not, you need to add this in
buildSrc/build.gradle[.kts]
:
Copy code
plugins {
    id("org.jetbrains.kotlin.jvm").version("1.5.30")
}
Also, if your plugin is in buildSrc already, you can apply it by class too:
Copy code
apply<BrowserStackPlugin>()
e
Also, if you are using Gradle >= 6, you can use precompiled script plugins so you don't need to write META-INF files and stuff
e
you don't have to write META-INF with
java-gradle-plugin
either, it will write it for you
Copy code
gradlePlugin {
    plugins {
       fooBar {
            id = "foo.bar"
            implementationClass = "..."
        }
    }
}
👌 1