Multiple catalogs multi-project buildSrc

Hello,
I’m playing around with multi project, buildSrc module as well as version catalogs.
I’m following the gradle guide on how to implement this.

I am facing two issues:

  • I can’t seem to name the catalog anything different from libs
  • I can’t create a second catalog, for example, a testLibs catalog like shown in the guide

Below, the relevant files:

  • buildSrc/settings.gradle.kts:
dependencyResolutionManagement {
    versionCatalogs {
        libs {
            from(files("../gradle/libs.versions.toml"))
        }
        testLibs {
            from(files('../gradle/test-libs.versions.toml'))
        }
    }
}
  • …/gradle/libs.versions.toml:
[versions]
springboot = "3.0.3"
spring-dependency-management = "1.1.0"
spring-docs = "2.0.4"

[libraries]
spring-docs = { module = "org.springdoc:springdoc-openapi-starter-webmvc-ui", version.ref = "spring-docs" }

[plugins]
org-springframework-boot = {id = "org.springframework.boot", version.ref = 'springboot'}
io-spring-dependency-management = { id = "io.spring.dependency-management", version.ref = 'spring-dependency-management' }
  • …/gradle/test-libs.versions.toml:
[versions]
junit = "5.9.1"

[libraries]
org-junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
  • buildSrc/src/main/groovy/tmp.java-common-conventions.gradle
dependencies {
    compileOnly libs.org.lombok
    annotationProcessor libs.org.lombok

    // Use JUnit Jupiter for testing.
    testImplementation testLibs.org.junit  // testLibs is not recognized
}

The build results in:

Build file '[...]/testing-layers/testing-layers-java/application/build.gradle' line: 2

An exception occurred applying plugin request [id: 'tmp.java-common-conventions']
> Failed to apply plugin 'tmp.java-common-conventions'.
   > groovy.lang.MissingPropertyException: Could not get unknown property 'testLibs' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

What am I missing ?

Gradle version: 8.1.1

Hi, i think this url might be worth reading. I am sad of gradle state with you also… afaik there is no elegant way. Only hack/workaround approach.

That’s not correct @peter-galcik.
He is building his convention plugin as a Groovy DSL precompiled script plugin, so he can use testLibs fine, if it were available in the build where he applies it.
The hack-around is only necessary for Kotlin DSL precompiled script plugins.

The problem is, that testLibs is only defined in buildSrc settings script.
But you need to also define it in the project where you apply the convention plugin to.
The naming convention for the default libs catalog is only valid for the libs catalog.
The testLibs catalog you have to define manually also in the settings script of the build where you want to use it.

1 Like

Thank you @Vampire , @peter-galcik
Looks like declaring testLibs in the root project is working.
Below the root settings.gradle:

dependencyResolutionManagement {
    versionCatalogs {
        testLibs {
            from(files('./gradle/test-libs.versions.toml'))
        }
    }
}

rootProject.name = 'testing-layers-java'

include 'domain'
include 'application'
include 'infrastructure'

I’m now able to use testLibs both in the sub projects as well as in the convention plugin.
I’m not clear though as to why the testLibs catalog needs “special treatment” compared to the libs catalog.

It does not.
The libs is what is special as it is defined automatically if the file is in the conventional place which it is for you.

1 Like