Skip to content

austinarbor/version-catalog-generator

Repository files navigation

Version Catalog Generator Plugin

ci codecov Gradle Plugin Portal

Easily use any BOM as a Gradle Version Catalog.

Compatibility

Gradle Version

Plugin Version

7.6.x

1.x

8.x

1.x, 2.x, 3.x

Quick Start

First, add your BOM dependencies to your version catalog

libs.versions.toml
[versions]
spring = "3.2.0"
aws = "2.22.0"

[libraries]
awsBom = { group = "software.amazon.awssdk", name = "bom", version.ref = "aws" }
springBootDependencies = { group = "org.springframework.boot", name = "spring-boot-dependencies", version.ref = "spring" }

Then, add the plugin to your settings with the catalogs you want to generate

settings.gradle.kts
import dev.aga.gradle.versioncatalogs.Generator.generate

plugins {
  id("dev.aga.gradle.version-catalog-generator") version("2.1.2")
}

dependencyResolutionManagement {
  repositories {
    mavenCentral() // (1)
  }
  versionCatalogs {
    generate("springLibs") { // (2)
      fromToml("springBootDependencies") { // (3)
        propertyOverrides = mapOf(
          "jackson-bom.version" to "2.16.1", // (4)
          "mockito.version" to versionRef("mockito"), // (5)
        )
        generateBomEntry = true // (6)
      }
    }
    generate("awsLibs") {
      fromToml("awsBom") {
        aliasPrefixGenerator = GeneratorConfig.NO_PREFIX // (7)
      }
    }
    generate("manyLibs") {
      using { // (8)
        aliasPrefixGenerator = GeneratorConfig.NO_PREFIX
      }
      fromToml("aws-bom", "jackson-bom") { // (9)
        aliasSuffixGenerator = { prefix, groupId, artifactId -> // (10)
          val trimmed = artifactId.replaceFirst("junit-", "").replaceFirst("jackson-", "")
          GeneratorConfig.DEFAULT_ALIAS_SUFFIX_GENERATOR(prefix, groupId, trimmed)
        }
      }
      from("org.springframework.boot:spring-boot-dependencies:3.4.1") { // (11)
        aliasPrefixGenerator = GeneratorConfig.DEFAULT_ALIAS_PREFIX_GENERATOR // (12)
      }
    }
  }
}
  1. Must include repositories here for dependency resolution to work from settings

  2. The name of the generated catalog

  3. The name of the bom library in the version catalog

  4. Optionally override some version properties using a literal value

  5. Or, you can reference version aliases in the source TOML

  6. Optionally generate an entry in the catalog for the BOM itself

  7. All dependencies in the AWS BOM are for AWS so we can skip the prefix

  8. Set the default generation options that will apply to all sources unless overridden

  9. Include all dependencies from both aws-bom and jackson-bom

  10. Override the default aliasSuffixGenerator for dependencies in these two BOMs

  11. Also include all dependencies from the spring-boot-dependencies BOM in the generated catalog

  12. Override the default aliasPrefixGenerator for dependencies in the spring BOM

Lastly, use the dependencies in your build

build.gradle.kts
dependencies {
  implementation(awsLibs.s3)
  implementation(awsLibs.dynamodb)
  implementation(springLibs.spring.springBootStarterWeb)
  implementation(springLibs.jackson.jacksonDatabind)
  implementation(manyLibs.sts)
  implementation(manyLibs.databind)
  implementation(manyLibs.spring.springBootStarterJdbc)
}

Detailed Usage

Goals

  • ✓ Compatible with Dependabot

  • ✓ Nested BOM support (i.e. spring-boot-dependences imports mockito-bom, etc)

  • ✓ Easy to override versions (similar to ext["version.property"] = …​ in Spring Boot Dependencies plugin)