Defining common dependencies and plugin versions for different projects using Gradle Kotlin DSL

Viewed 452

I have a collection of related Gradle projects, not all of them in the same repository (hence not sharing the same gradle.properties or settings.gradle.kts files). My objective is having my main build scripts (build.gradle.kts files) completely clean from dependency version numbers and at the same time avoiding to specify the same version number in more than once settings or properties file.

I am looking for the best practices to accomplish this, taking into consideration that these version numbers can appear both in Gradle dependencies or plugin declarations and often need to be kept in sync between them.

As a use case, lets configure projects that require spring boot. In this case, I can use a Gradle platform project to define common dependency versions. As an example, the build.gradle.ktsof my platform project looks a bit like this:

plugins {
    `java-platform`
}

javaPlatform {
    allowDependencies()
}

dependencies {
    api(platform("org.springframework.boot:spring-boot-dependencies:2.3.0.RELEASE"))

    constraints {
        api("io.mockk:mockk:1.10.0")
        api("org.junit.jupiter:junit-jupiter:5.6.2")
        api("io.kotest:kotest-runner-junit5-jvm:4.0.6")
        ...
    }
}

So I am conveniently specifying all the spring boot dependencies + my own dependencies in one single platform project.

But other projects which use spring boot dependencies also require the org.springframework.boot plugin, where its version should better be the same than the spring boot dependency required in the platform project.

As said at the beginning, those projects are located in other repositories, hence they do not share the same configuration files as the platform project. In those projects I am adding in their settings.gradle.kts file the following block:

pluginManagement {
    plugins {
        id("org.springframework.boot").version("2.3.0.RELEASE")
        ...
    }
}

It works, since after importing the platform project I can keep my main build file clean from version numbers, as intended (neither the spring boot dependencies nor the spring boot plugin itself will require anymore to specify their versions).

However, this just does not feel good. Now I have the same version of spring boot (2.3.0.RELEASE) hard-coded in both my platform project and in the settings file of the project using the Spring boot plugin. Now I have the problem of keeping both numbers, located in distinct projects, synchronized.

Which is the "correct" Gradle way to solve this? Is there a way to make a Gradle platform project to also specify plugin version numbers in a similar way preferred dependency versions can be declared there?

0 Answers
Related