Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using version catalog in Gradle Kotlin build for subprojects

I am migrating a Gradle multi module build from Groovy to Kotlin DSL and I would like make use of Gradle's static typed version catalogue.

// plugins declared in the settings.gradle.kts

subprojects {

  apply {
    plugin("groovy")
    plugin("java-library")
    plugin("ch.onstructive.gradle.release")
    plugin("ch.onstructive.gradle.codestyle")
  }

  configure<JavaPluginExtension> {
    sourceCompatibility = JavaVersion.toVersion("17")
    targetCompatibility = JavaVersion.toVersion("17")
  }

  dependencies {

    val annotationProcessor by configurations
    val implementation by configurations

    annotationProcessor(libs.mapstruct.processor) // <-- cannot access the libs here
    implementation(libs.mapstruct) // <-- cannot access the libs here
  }
}

And in my gradle/libs.versions.toml

[versions]
mapstructVersion = "1.5.5.Final"

[libraries]
mapstruct-processor = { module = "org.mapstruct:mapstruct-processor", version.ref = "mapstructVersion" }
mapstruct = { module = "org.mapstruct:mapstruct", version.ref = "mapstructVersion" }

Now I'm getting the following error

Build file 'build.gradle.kts' line: 54

Extension with name 'libs' does not exist. Currently registered extension names: [ext, base, defaultArtifacts, sourceSets, reporting, javaToolchains, java, groovyRuntime, testing, publishing, spotless, micronautSourceSetConfigurer, micronaut, eclipse, graalvmNative]

How can I use the version catalogue in the dependencies closure of the subprojects closure?

like image 931
saw303 Avatar asked Oct 16 '25 03:10

saw303


1 Answers

In this case the version catalogue can be accessed using the rootProject.libs.

subprojects {

  apply {
    plugin("groovy")
    plugin("java-library")
    plugin("ch.onstructive.gradle.release")
    plugin("ch.onstructive.gradle.codestyle")
  }

  configure<JavaPluginExtension> {
    sourceCompatibility = JavaVersion.toVersion("17")
    targetCompatibility = JavaVersion.toVersion("17")
  }

  dependencies {

    val annotationProcessor by configurations
    val implementation by configurations

    annotationProcessor(rootProject.libs.mapstruct.processor)    
    implementation(rootProject.libs.mapstruct)
  }
}
like image 69
saw303 Avatar answered Oct 18 '25 20:10

saw303



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!