How to use version catalog in the root `settings.gradle.kts` file

Hi, I was later figured how to do that indirectly through creating a convention settings plugin, inspired by this video Understanding Gradle #15.1 – Full Java Project Setup - YouTube

I will use the Gradle Enterprise Plugin as example.

So first, in the settings.gradle.kts file, enable including local convention plugins by:

// in the root folder settings.gradle.kts
pluginManagement {
  repositories {
    gradlePluginPortal()
    // other repo
  }
  includeBuild("gradle/settings") // include my own plugins
}

Then in the gradle/settings folder, create a settings.gradle.kts that includes a version catalog:

// in the gradle/settings folder settings.gradle.kts
dependencyResolutionManagement {
  versionCatalogs {
    create("libs") {
      from(files("relative/path/to/libs.versions.toml"))
    }
  }
}

include("my-settings-plugins")

Since we do include("my-settings-plugins"), we want to create a convention settings plugin in gradle/settings/my-settings-plugins, so create a build.gradle.kts first:

// gradle/settings/my-settings-plugins/build.gradle.kts
plugins {
  `kotlin-dsl`
}

dependencies {
  implementation(libs.gradle.enterprise) // consuming the version catalog here
}

Now we can start writing the convention plugin, so create a my.settings-plugin.settings.gradle.kts in gradle/settings/my-settings-plugins/src/kotlin/main:

// gradle/settings/my-settings-plugins/src/kotlin/main/my.settings-plugin.settings.gradle.kts
plugins {
  id("com.gradle.enterprise")
}

// optionally include this 
gradleEnterprise {
  // ... 
}

Finally, in your version catalog, you can add

[libraries]
# some other libraries ...
gradle-enterprise = "com.gradle.enterprise:com.gradle.enterprise.gradle.plugin:3.12.6"

Finally, the root settings.gradle.kts can be simplified as:

// in the root folder settings.gradle.kts
plugins {
  // id("com.gradle.enterprise") version ("3.12.1")
  id("my.settings-plugin") // the id must match the file name prefix of `my.settings-plugin.settings.gradle.kts` 
}
1 Like