Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version catalogs should work in buildscript dependencies #16958

Closed
swankjesse opened this issue Apr 23, 2021 · 9 comments
Closed

Version catalogs should work in buildscript dependencies #16958

swankjesse opened this issue Apr 23, 2021 · 9 comments

Comments

@swankjesse
Copy link

This was previously reported as part 2 of Issue 16652.

Expected Behavior

I expect centralized dependency versions to work everywhere I specify dependencies. This includes my buildscript clause.

In myproject/settings.gradle.kts:

enableFeaturePreview("VERSION_CATALOGS")
...

In myproject/gradle/libs.versions.toml:

[libraries]
shadowJarPlugin = "com.github.jengelman.gradle.plugins:shadow:6.1.0"

In myproject/build.gradle.kts:

buildscript {
  dependencies {
    classpath(libs.shadowJarPlugin)
  }
}
...

Current Behavior

> Configure project :
e: /Users/jwilson/Development/myproject/build.gradle.kts:20:15: Unresolved reference: libs

Context

I’m using Gradle 7.

Workaround

This is a workaround. It is verbose, doesn’t have IDE support, and is inconsistent with my other dependencies blocks.

buildscript {
  fun libs(lib: String): String {
    val versionCatalog = project.extensions.getByType(VersionCatalogsExtension::class).named("libs")
    val dep = versionCatalog.findDependency(lib).get().get()
    return "${dep.module.group}:${dep.module.name}:${dep.versionConstraint.displayName}"
  }

  dependencies {
    classpath(libs("shadowJarPlugin"))
  }
}
@PaulWoitaschek
Copy link

@swankjesse
Better workaround that allows for typesafely accessing the versions:

buildscript {
  repositories {
    gradlePluginPortal()
    google()
    mavenCentral()
  }
  dependencies {
    val libs = project.extensions.getByType<VersionCatalogsExtension>().named("libs") as org.gradle.accessors.dm.LibrariesForLibs
    classpath(libs.sqlDelight.gradlePlugin)
  }
}

@tpodg
Copy link

tpodg commented Apr 29, 2021

I have a similar problem, but in a different place. I tried to define dependencies from service catalog for sub-projects and it does not work either.

Example:

subprojects {
   dependencies {
      implementation(libs.abc)
   }
}

Extension with name 'libs' does not exist. Currently registered extension names: ...

@rock3r
Copy link

rock3r commented May 21, 2021

I have a similar problem, but in a different place. I tried to define dependencies from service catalog for sub-projects and it does not work either.

Example:

subprojects {
   dependencies {
      implementation(libs.abc)
   }
}

Extension with name 'libs' does not exist. Currently registered extension names: ...

Looks like you're having this issue, which is unrelated

@piyush-kukadiya
Copy link

@swankjesse Is this feature VERSION CATALOG supported by Android Studio? I tried it but autocomplete ain't working.

@jeffdcamp
Copy link

What version of Gradle should this fix show up in? (I just tested Gradle 7.1 and this seems to still be broken)

@krzdabrowski
Copy link

krzdabrowski commented Jun 15, 2021

I just tested it too with 7.1 and it doesn't work yet. I guess it was fixed too late to come into 7.1. And also some docs for that feature are waiting for review. Hopefully it will be included in next release.

EDIT: it will be in 7.2, see milestones.

@PaulWoitaschek
Copy link

Can you write a proper issue description? "Sill not working" is very hard to debug.

@htueko
Copy link

htueko commented Oct 13, 2021

I make several toml files according to usage.

in myproject/gradle/gradle.versions.toml

[versions]
gradleVersion = "7.0.3"
[libraries]
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradleVersion" }

in myproject/gradle/kotlin.versions.toml

[versions]
kotlin = "1.5.30"
[libraries]
kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlinx-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }

in myproject/gradle/qc.versions.toml

[versions]
spotlessVersion = "5.17.0"
[libraries]
spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "spotlessVersion" }
[plugins]
spotless = {id = "com.diffplug.spotless", version.ref = "spotlessVersion"}

in myproject/settings.gradle.kts
enableFeaturePreview("VERSION_CATALOGS") dependencyResolutionManagement { versionCatalogs { // gradle related create("gradleLibs") { from(files("gradle/gradle.versions.toml")) } // kotlin related create("kotlinLibs") { from(files("gradle/kotlin.versions.toml")) } // code quality libraries create("qcLibs") { from(files("gradle/qc.versions.toml")) } } }

project level gradle file
in myproject/build.gradle.kts

classpath(gradleLibs.gradle) classpath(kotlinLibs.kotlin) classpath(qcLibs.plugins.spotless)

first time it's work, then gradleLibs, kotlinLibs and qcLibs give me error, Unsolved reference

@konnovdev
Copy link

Paul's answer in Groovy

        def libs = project.extensions.getByType(VersionCatalogsExtension).named("libs") as org.gradle.accessors.dm.LibrariesForLibs
        classpath(libs.gradlePluginHiltAndroid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants