Search code examples
gradlegradle-kotlin-dsl

What do the backticks in build.gradle.kts do?


I have the following in my build.gradle.kts Gradle build file:

plugins {
    `kotlin-dsl`
    id("org.jetbrains.kotlin.jvm") version "1.6.10"
    id("maven-publish")
    `java-gradle-plugin`
}

Of particular interest is the kotlin-dsl and java-gradle-plugin. I understand that backticks enable you to create identifiers that are normally invalid syntax. So in this case it's the - in the name makes this required. But what exactly is happening here when I put those lines in? I'm just typing out that variable. I'm not assigning it to anything, using as an argument to a function, or doing anything that looks like I'm configuring something.

What is actually happening on those lines?


Solution

  • Same thing, different way to apply it in Kotlin. id(...) applies based on a string. May be required for less well-known plugins. Without the id, you're applying the plugin "directly", so to speak.

    You can see here, this is the recommended method for application according to Gradle docs:

    https://docs.gradle.org/current/userguide/java_gradle_plugin.html

    You can apply the maven-publish plugin the same way--make it not a string, and surround with back-ticks (since the dash would break that one as well).


    Internally, by introspecting into the method from within IntelliJ, it appears that the explicit implementation is merely a convenience method which has an inline that calls id(...) for you.

    /**
     * The builtin Gradle plugin implemented by [org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin].
     *
     * @see org.gradle.plugin.devel.plugins.JavaGradlePluginPlugin
     */
    inline val org.gradle.plugin.use.PluginDependenciesSpec.`java-gradle-plugin`: org.gradle.plugin.use.PluginDependencySpec
        get() = id("org.gradle.java-gradle-plugin")