How to use org.gradle.java-gradle-plugin in build.gradle.kts kotlin build script?

I’m experimenting with turning one of my build.gradle files for a project that builds gradle plugins into a build.gradle.kts and so far struggling.

org.gradle.java-gradle-plugin creates a ‘gradlePlugin’ extension.
In a build.gradle, I configure it like so:

plugins {
    groovy
    java-gradle-plugin
}
gradlePlugin {
    plugins {
        simplePlugin {
            id = "com.foo.bar"
            implementationClass = 'com.foo.bar.PluginImpl'
        }
    }
}
... // Repositories, dependencies, etc.

In my attempted build.gradle.kts conversion:

plugins {
    groovy
    `java-gradle-plugin`
}

gradlePlugin {
// unresolved reference. How do I configure this thing ??
}

Do I need to do some kind of project.extensions.getByType() or delegateClosureOf<SomeClassFromWithinThePlugin> thing to get access to the DSL added by the java-gradle-plugin extension?

1 Like

You would use configure<> for plugin extensions. Use the fully qualified class name (or imports), at least for non-core classes. Your equivalent Kotlin build script could look like this:

plugins {
    groovy
    `java-gradle-plugin`
}

configure<GradlePluginDevelopmentExtension> {
    plugins {
        create("simplePlugin") {
            id = "com.foo.bar"
            implementationClass = "com.foo.bar.PluginImpl"
        }
    }
}
1 Like

This is not necessary with the latest Kotlin DSL version. What @Sam1 wrote should just work. Please make sure you are using both the latest Kotlin DSL as well as the latest Kotlin plugin for IntelliJ.

Thanks @jjustinic, that works! Although the syntax isn’t as terse/pretty as I’d hoped after seeing all the great presentations on Kotlin at the Gradle Summit. Rummaging through a plugin’s source to identify which class(es) are applies as extensions isn’t ideal.

@st_oehme, The command line and IntelliJ are in agreement that “gradlePlugin” is an “unresolved reference”.
I’m using the release version of Gradle 4.0 and have not specified anything anything about kotlin except appending ‘.kts’ the name of my build file. A build scan shows that I’m using the version of the Kotlin DSLs that comes with Gradle itself. Is it possible to specify a different version of the Kotlin DSLs without changing to a pre-release version of Gradle?

@rodrigo which version of the Kotlin DSL did we ship with 4.0? Did it already support auto-generated accessors with the plugins DSL?

@Sam1 setup instructions are in the Kotlin DSL repo.

The Kotlin DSL repo’s documentation shows the Configure<Class> syntax and the samples I’ve looked at all use a pre-release version of Gradle (gradle-kotlin-dsl-4.1-20170707032407+0000-all.zip, in the linked example).

If there’s a way to opt-in to the latest release versions of the Kotlin DSLs without having to use pre-release Gradle I’d love to try it out!

The syntax that would require auto-generated accessors starts working for me as of gradle-kotlin-dsl-4.1-20170617005006+0000-bin.zip, which shows to be a snapshot of gradle-kotlin-dsl 0.10.0. According to core, 0.9.0 shipped with 4.0, and 0.9.1 is the latest in master, so it doesn’t look like there’s a published combo of released versions that can support this out of the box quite yet.

Looks like I have another thing to look forward to from Gradle’s 4.1 release, then! Thanks again, James.

I have no knowledge of the actual release plans, but from the commit history, Gradle 4.0 -> 4.1 provides the Kotlin DSL 0.9.0 -> 0.9.1 update currently. The master branch is already for Gradle 4.2. I would hold off getting your hopes up for Gradle 4.1 until the update to 0.10.+ is committed (or there proves to be an easy opt-in to newer Kotlin DSL versions like you asked about).

1 Like

The version shipped with Gradle 4.0 was 0.9.1 which already supported auto-generated accessors except they need to be explicitly enabled via a Gradle property.

FTR: Gradle 4.1 will ship with 0.10.+.

2 Likes

Hm, that property doesn’t seem to affect my project

misc-gradle-tasks\build.gradle.kts:41:1: Unresolved reference: gradlePlugin

My full gradle.properties file:

version=1.6
org.gradle.script.lang.kotlin.accessors.auto=true

Also, good job on your presentations on Gradle + Kotlin at the Gradle Summit, Rodrigo! I enjoyed them - enough to give Kotlin buildscripts a try in the first place.