Home > Software design >  Declare Gradle buildSrc plugin using Kotlin DSL
Declare Gradle buildSrc plugin using Kotlin DSL

Time:12-01

I'm trying to figure out how to convert this configuration to the Kotlin DSL, but I can't find much in the way of examples:

gradlePlugin {
    plugins {
        javaConventionsPlugin {
            id = "build.java-conventions"
            implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
        }
    }
}

What would this declaration look like using Kotlin?

CodePudding user response:

It is documented in the guide: https://docs.gradle.org/current/userguide/java_gradle_plugin.html#sec:gradle_plugin_dev_usage

The way you have also works. Any of the following would also work:

gradlePlugin {
    plugins {
        register("javaConventionsPlugin") {
            id = "build.java-conventions"
            implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
        }
    }
}

gradlePlugin {
    plugins {
        create("javaConventionsPlugin") {
            id = "build.java-conventions"
            implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
        }
    }
}

The former uses Gradle's lazy configuration.

CodePudding user response:

This is what I've found so far, not sure if there's a more fluent way to do it:

gradlePlugin {
    val javaConventionsPlugion = plugins.register("javaConventionsPlugin")
    javaConventionsPlugion.configure {
        id = "build.java-conventions"
        implementationClass = "buildlogic.plugins.JavaConventionsPlugin"
    }
}
  • Related