Gradle 7.0 Version Catalog for maven bom
Asked Answered
S

3

25

I have published maven bom and imported it in top level build.gradle.kts as:

allProjects {
  dependencies {
        implementation(platform("com.example:some-dependencies:1.2.3"))
    }
}

And then in libs.versions.toml:

[libraries]
some-bom = { group = "com.example", name="some-dependencies", version="1.2.3" }

When I change first code sample to:

allProjects {
  dependencies {
        implementation(platform(libs.some.bom))
    }
}

I get:

Could not resolve: javax.xml.bind:jaxb-api
Could not resolve: org.springframework.boot:spring-boot-starter-test
...

Is there any way to use Gradle 7 version catalogs with boms?

Stewardess answered 21/4, 2021 at 9:15 Comment(0)
T
47

In my case, it just worked. I'm working on Android project and my script is just like below:

//libs.versions.toml
[libraries]
deps_okhttp_bom = "com.squareup.okhttp3:okhttp-bom:4.9.1"
deps_okhttp_lib = { module  ="com.squareup.okhttp3:okhttp" }
deps_okhttp_logging_interceptor = { module= "com.squareup.okhttp3:logging-interceptor"}

//build.xml
dependencies {
  implementation platform(libs.deps.okhttp.bom)
  implementation libs.deps.okhttp.lib
  implementation libs.deps.okhttp.logging.interceptor
}

In your example, you just added dependency for BOM. But as BOM is just an spec sheet which describes versions for each libraries, you need to add dependencies for specific libraries.

Typewriting answered 11/5, 2021 at 11:26 Comment(0)
B
4

Another example usage with bundles:

TOML file:

[versions]

# AndroidX - Compose
androidxComposeBom = "2024.02.01"

[libraries]

# AndroidX - Compose
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidxComposeBom" }
androidx-compose-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-compose-ui-util = { group = "androidx.compose.ui", name = "ui-util" }
androidx-compose-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-compose-material = { group = "androidx.compose.material", name = "material" }

[bundles]

# AndroidX - Compose
androidx-compose-bom = ["androidx-compose-ui", "androidx-compose-ui-util", "androidx-compose-ui-tooling-preview", "androidx-compose-material"]

build.gradle.kts file:

dependencies {
    implementation(platform(libs.androidx.compose.bom))
    implementation(libs.bundles.androidx.compose.bom)
}
Borroff answered 4/3, 2024 at 11:18 Comment(0)
M
1

Late to the party here, but I created a Gradle settings plugin to handle this scenario specifically with minimal boilerplate. All you do is specify the name of the library alias in your TOML, and it will read the BOM and generate library entries for you so you don't have to manually specify them

https://github.com/austinarbor/version-catalog-generator

settings.gradle.kts

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

dependencyResolutionManagement {
    repositories {
        mavenCentral() // must include repositories here for dependency resolution to work from settings
    }
    versionCatalogs {
        generate("springLibs") { // the name of the generated catalog
            from(toml("springBootDependencies")) // name of the bom library in the version catalog
            propertyOverrides = mapOf(
                "jackson-bom.version" to "2.16.1", // optionally override some version properties using a literal value
                "mockito.version" to versionRef("mockito"), // or you can reference version aliases in the source toml
            )
        }
        generate("awsLibs") { 
            from(toml("awsBom"))
            // all dependencies in the aws bom are for aws so we can skip the prefix
            aliasPrefixGenerator = GeneratorConfig.NO_PREFIX
        }
    }
}

build.gradle.kts

// now you can use the generated libraries/entries in your dependencies
dependencies {
    implementation(awsLibs.s3)
    implementation(awsLibs.dynamodb)
    implementation(springLibs.spring.springBootStarterWeb)
    implementation(springLibs.jackson.jacksonDatabind)
}
Melville answered 3/3, 2024 at 19:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.