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?

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.

1 Like

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)

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.

1 Like