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

I want to use the Gradle Enterprise Plugin so that my GitHub CI can run with build scan using --scan. However, based on official doc, this plugin must be applied in the root settings.gradle.kts file. So right now I have the following in the root settings.gradle.kts file:

// in my root settings.gradle.kts file
dependencyResolutionManagement {
  repositories {
    // various repos
  }
  includeBuild("gradle/platform")
  // gradle/libs.versions.toml is automatically imported if exists
}

plugins {
  id("com.gradle.enterprise") version ("3.12.1")
}

gradleEnterprise {
  buildScan {
    termsOfServiceUrl = "https://gradle.com/terms-of-service"
    termsOfServiceAgree = "yes"
  }
}

My question is: How to version catalog in the root settings.gradle.kts file, so that I can do somthing like this:

// in my root settings.gradle.kts file
dependencyResolutionManagement {
  repositories {
    // various repos
  }
  includeBuild("gradle/platform")
  // gradle/libs.versions.toml is automatically imported if exists
}

plugins {
  alias(libs.plugins.gradle.enterpise) // using verion catalog
}

gradleEnterprise {
  buildScan {
    termsOfServiceUrl = "https://gradle.com/terms-of-service"
    termsOfServiceAgree = "yes"
  }
}

Any idea how to archive this?

1 Like

You cannot.
That’s a hen-and-egg problem.
The feature is designed, so that a settings plugin could define a version catalog.
But that means that the version catalog can not define settings plugins because for that the version catalog would need to be available before the plugins are evaluated.
To define it in the version catalog, you would need to manually parse the TOML via some library or regex or whatever to pull out the information necessary to apply the plugin.

3 Likes

Thanks for answering. So the version catalog is defined by the settings.gradle.kts, hence the catalog would not be available before settings.gradle.kts get parsed. That’s correct?

And do you know in any chance that what Gradle APIs or libraries I can use to parse a TOML file? (Either documentation or sample code)

1 Like

Correct.

There are no public Gradle APIs for parsing the TOML.
You can use any TOML library out there, it is a standard.
I don’t have one at hand as I didn’t do manual TOML parsing so far.

2 Likes

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