Trouble using centralized dependency versions in buildSrc plugins and buildscript classpath

The new centralized dependency versions feature preview in Gradle 7 is pretty great, but there are a couple of places where libs doesn’t resolve.

  1. In plugins in buildSrc, e.g. buildSrc/src/main/kotlin/MyPlugin.kt:

    class MyPlugin : Plugin<Project> {
    
        override fun apply(project: Project): Unit = project.run {
    
            dependencies {
                implementation(libs.groovy)
                testImplementation(libs.junit)
                [...]
            }
            [...]
        }
    }
    

    The error I get ise: .../buildSrc/src/main/kotlin/MyPlugin.kt: (21, 32): Unresolved reference: libs

  2. In dependencies/classpath in the root build.gradle.kts:

    buildscript {
        [...]
        dependencies {
            classpath(libs.gradleVersionsPlugin)
            [...]
        }
    }
    

    Error is Script compilation error: Line 27: classpath(libs.gradleVersionsPlugin) Unresolved reference: libs

  3. In the root settings.gradle.kts:

    plugins {
        id("com.gradle.enterprise").version(libs.versions.gradleEnterprise.get())
    }
    

    Throws approximately the same error as in pt. 2.

  4. In the plugins block in buildSrc/build.gradle.kts:

    plugins {
        id("org.jmailen.kotlinter").version(libs.versions.kotlinter.get())
    }
    

    Throws approximately the same error as in pt. 2.

Is there a way to solve any of these?

SEO: enableFeaturePreview("VERSION_CATALOGS")

Hi @neu242 !

You can’t use type-safe catalogs in plugins, for the reason explained in this ticket: Make version catalogs accessible from precompiled script plugins · Issue #15383 · gradle/gradle · GitHub

Basically if you use libs in a plugin, precompiled or not, we have to assume that it’s going to be applied to a project which has exactly the same model, which might not be the case.

There is, however, a “type-unsafe” API to access the catalogs: Provide access to version catalogs from plugins by melix · Pull Request #15443 · gradle/gradle · GitHub

  1. buildscript blocks as well as the plugins block are evaluated before the project extensions are loaded, so before the catalogs are available. It’s not possible yet to use catalogs for this purpose. The initial implementation to support versions in plugins has been removed, but we’re planning more work on this.

  2. the settings file is the one loading/defining catalogs. This means that the plugins of the settings file itself cannot use catalogs, because they are not available yet. Chicken and egg problem.

  3. same problem here

1 Like

Hi @CedricChampeau :slight_smile:

This makes sense. Thanks for the solution to (1). This seems to work:

fun libs(lib: String) =
  project.extensions.getByType(VersionCatalogsExtension::class).named("libs").findDependency(lib).get()

dependencies {
  implementation(libs("gson"))
}

Looking forward to a solution to (2) as well.

And thanks for great answers!