Gradle BOM project

Viewed 1486

I am working on a task where parent project with all the required dependencies are mentioned in gradle and this project to be used as parent, basically a BOM which has all the components mentioned and can be usedrefered as dependency in other project

Say for example, Spring boot parent and other dependencies are mentioned in gradle file, which I can refer this project in other projects.

The projects are not in multi module structure.

Maven equivalent:

<dependencyManagement>
<dependencies>
  <dependency>
 </dependency>
</dependencies>
</dependencyManagement>

similar tags - pluginManagement, build, profiles etc

If you can refer me to the resources or some head start would be helpful.

Thanks.

1 Answers

Sharing common dependency version across projects can be accomplished using a platform:

A platform is a special software component which can be used to control transitive dependency versions. In most cases it’s exclusively composed of dependency constraints which will either suggest dependency versions or enforce some versions. As such, this is a perfect tool whenever you need to share dependency versions between projects.

With the Java Platform plugin, you can create a project to accomplish this (Gradle does not have a 1:1 equivalent of parent):

// build.gradle
// Let's call this project com.example:my-parent-bom

plugins {
    id 'java-platform'
}

javaPlatform {
    allowDependencies()
}

dependencies {
    // Import other BOMs
    api platform('org.springframework.boot:spring-boot-dependencies:2.4.4')
    api platform('org.springframework.cloud:spring-cloud-dependencies:2020.0.2')

    // Define version for dependencies not managed by the above BOMs
    constraints {
        api 'commons-httpclient:commons-httpclient:3.1'
        runtime 'org.postgresql:postgresql:42.2.5'
    }
}

Then you can publish this platform like any other Gradle project and consume it:

// some other build.gradle

plugins {
    id 'java'
}

dependencies {
    implementation platform('com.example:my-parent-bom')
}
Related