Search code examples
kotlingradlegradle-kotlin-dsl

In Gradle kts, how to fix the error "Plugin requests from precompiled scripts must not include a version number."


I have the following gradle kts build file:

plugins {
    base
    java
    kotlin("jvm") version "2.0.20"
    `java-test-fixtures`

    `project-report`
    idea

    id("com.github.ben-manes.versions")
}

upon compiling, it causes the following error:



FAILURE: Build failed with an exception.

* Where:
/home/peng/git-template/scaffold-gradle-kts/buildSrc/src/main/kotlin/ai.acyclic.java-conventions.gradle.kts line: 1

* What went wrong:
Invalid plugin request [id: 'org.jetbrains.kotlin.jvm', version: '2.0.20']. Plugin requests from precompiled scripts must not include a version number. Please remove the version from the offending request and make sure the module containing the requested plugin 'org.jetbrains.kotlin.jvm' is an implementation dependency of project ':buildSrc'.

what's the meaning of this? How to fix it?


Solution

  • Indeed, plugin requests in precompiled plugin scripts cannot contain a version number.

    This is because these scripts instead pick up their plugins only from dependencies that are on their compilation classpath.

    So when you apply the Kotlin JVM plugin in the plugins block, you need to apply it without a version:

    kotlin("jvm")
    

    And rather than the plugins block downloading that plugin's artifact directly, it is provided via that plugin's build; in other words in the build.gradle.kts file compiling that plugin, you need:

    repositories {
        gradlePluginPortal() // or mavenCentral()
    }
    
    dependencies {
        implementation("org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:2.0.20")
    }
    

    If you were wondering how to find these coordinates you can find them on the plugin's page at the Gradle Plugin Portal within the "Adding the plugin to build logic for usage in precompiled script plugins" section.

    Moreover you can read the associated documentation on applying plugins in precompiled script plugins here.