Search code examples
gradledependenciesversioncatalog

Getting unknown property error when I try to use new Gradle 7 version catalogs


I tried to implement the simplest possible example to better understand Gradle 7 version catalogs. I used 'gradle init' to generate a new application, then I followed the steps presented in this blog post -> https://melix.github.io/blog/2021/03/version-catalogs.html exactly as I could. The result, when I tried to import the project into Intellij (and also when I simply ran 'gradlew test' at root directory of project) was the error below. I'm guessing it is more likely that the feature works and I did something dumb, than the feature is broken. Any sharp pair of eyes that can help me spot the dumb thing?

ERROR I GOT

A problem occurred evaluating project ':app'.

Could not get unknown property 'testDependencies' for extension 'libs' of type org.gradle.accessors.dm.LibrariesForLibs.

WHAT I DID

  1. Generate a simple Gradle example app via gradle init, as below:

    Select type of project to generate:
      1: basic
      2: application
      3: library
      4: Gradle plugin
    Enter selection (default: basic) [1..4] 2<RETURN>

    Select implementation language:
      1: C++
      2: Groovy
      3: Java
      4: Kotlin
      5: Scala
      6: Swift
    Enter selection (default: Java) [1..6] 5<RETURN>

    Split functionality across multiple subprojects?:
      1: no - only one application project
      2: yes - application and library projects
    Enter selection (default: no - only one application project) [1..2] 1<RETURN>

    Select build script DSL:
      1: Groovy
      2: Kotlin
    Enter selection (default: Groovy) [1..2] 1<RETURN>

    Project name (default: stacko):<RETURN>
    Source package (default: stacko):<RETURN>
  1. Add the version catalog definition where they say it should go (exact content as in blog post)
    cat <<EOF >gradle/libs.versions.toml 
    [libraries]
    guava = "com.google.guava:guava:30.0-jre"
    junit-jupiter = "org.junit.jupiter:junit-jupiter-api:5.7.1"
    junit-engine = { module="org.junit.jupiter:junit-jupiter-engine" }

    [bundles]
    testDependencies = ["junit-jupiter", "junit-engine"]
    EOF
  1. Enable the preview feature
    echo "enableFeaturePreview('VERSION_CATALOGS')" >/tmp/stuff
    cat settings.gradle >> /tmp/stuff
    cp /tmp/stuff settings.gradle 

Then I added this (WRONG) line as the last line in my dependencies { } block (see accepted answer for correction):

testImplementation(libs.testDependencies)
  1. Tried to import into Intellij and got the error shown in ERROR I GOT, above.

Solution

  • Replace libs.testDependencies with libs.bundles.testDependencies and it'll work. Maybe a typo in the blog post or a last minute change. The Dependency Bundles documentation is correct.