How to pbulish a BOM out of gradle subprojects?

Tags: , , ,



I have a multi module gradle build which a collection of reusable component projects and a collection of apps that use the common components.

.
├── build.gradle.kts
├── components
│   ├── blob
│   ├── clock
│   ├── database
│   ├── email
│   ├── json
│   └── web
├── gradle.properties
├── platform
│   ├── build.gradle.kts
│   └── settings.gradle.kts
├── settings.gradle.kts
└── app
    ├── api
    └── ui

In the example tree structure above the app:api project can use the components:clock project. I want to publish a Bill of Material (BOM) from all the component projects so that I can move components into it’s own repo.

How can I configure gradle to publish a BOM out of a collection of sub projects?

Answer

Gradle provides a special plugin to generate BOM artifacts (called Platforms in the Gradle world)

You can declare them using the java-platform plugin.

https://docs.gradle.org/current/userguide/java_platform_plugin.html

You can create an additional subproject that references all of your components projects. The maven-publish plugin works out of the box pretty much the same as with regular java projects and will publish a BOM POM.

I don’t have any samples in Kotlin syntax at hand, but basically the declaration looks pretty much the same as with regular dependencies but with an additional constraints block:

plugins {
    `java-platform`
}

dependencies {
    constraints {
        api project(":lib")
    }
}


Source: stackoverflow