Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use version catalog inside precompiled Gradle plugin

I am converting my project with multiple modules to use Precompiled Gradle Plugins. This project also uses Gradle's version catalog (libs.versions.toml) where the documentation explicitly shows how to share it with the build.gradle.kts of the buildSrc directory, which works fine. But unfortunately this does not apply to the plugins themselves. Example:

build-logic/settings.gradle.kts

rootProject.name = "build-logic"

dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from(files("../gradle/libs.versions.toml"))
        }
    }
}

build-logic/build.gradle.kts:

plugins {
    `kotlin-dsl`
    `kotlin-dsl-precompiled-script-plugins`
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(libs.plugins.kotlin.jvm) // works fine
}

build-logic/src/main/kotlin/my-template.gradle.kts

plugins {
    id("org.jetbrains.kotlin.jvm")
}

repositories {
    mavenCentral()
}

dependencies {
    implementation(libs.kotlinx.serialization) // "Unresolved reference: libs"
}

Is it possible to reference "libs" inside my-template.gradle.kts?

Thank you

like image 627
Maddin Avatar asked Oct 21 '25 03:10

Maddin


1 Answers

So there is an open issue about this request and a workaround has been found. To use libs inside the precompiled plugin you have to add the following:

build-logic/build.gradle.kts

dependencies {
    implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

build-logic/src/main/kotlin/my-template.gradle.kts

import org.gradle.accessors.dm.LibrariesForLibs

val libs = the<LibrariesForLibs>()

dependencies {
    implementation(libs.kotlinx.serialization) // works now!
}

Credit

like image 148
Maddin Avatar answered Oct 22 '25 23:10

Maddin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!