Hi! Is it possible to use a Kotlin plugin inside a...
# gradle
j
Hi! Is it possible to use a Kotlin plugin inside a plugin script located in
buildSrc
? I've created a simple build with the following structure: • project
lib
• project
app
(depends on
lib
) both projects will be written in Kotlin, so I wanted to create a plugin script to group some common configuration, here is the content of `buildSrc/src/main/kotlin/kotlin-conventions.gradle.kts`:
Copy code
plugins {
    kotlin("jvm")
}
but, when I run it, I get the following error:
Copy code
Plugin [id: 'org.jetbrains.kotlin.jvm'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
and if I try to set a version with
kotlin("jvm") version "1.8.10"
, I get:
Copy code
Invalid plugin request [id: 'org.jetbrains.kotlin.jvm', version: '1.8.10']. 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'.
Here is a reproducible example, you just have to download the zip file and run
./gradlew
1
a
you’ll need to add the Kotlin plugin as a regular dependency in
buildSrc/build.gradle.kts
I find the easiest way is to find the plugin in the Gradle Plugin Portal, and look at the ‘legacy’ method. It will have
classpath(<maven-coordinates>)
. Use those coordinates as the dependency, like so:
Copy code
// buildSrc/build.gradle.kts
plugins {
  `kotlin-dsl`
}

dependencies {
  implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
}
e
j
works like a charm, thank you @Adam S!
v
@Adam S you find that easier than to simply use
<pluginid>:<pluginid>.gradle.plugin:<pluginversion>
?
147 Views