Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define dependency version only once for whole Gradle multi-module project?

I made a decision to migrate from Dependency Management Plugin to Gradle built-in BOM import support. Since Gradle built-in BOM import support has better performance But

I run into the issue:

I cannot find alternatives for dependency and dependencySet in native Gradle:

    dependencyManagement {
        dependencies {
            dependency("org.springframework:spring-core:4.0.3.RELEASE")
        }
    }
//or
    dependencyManagement {
         dependencies {
              dependencySet(group:'org.slf4j', version: '1.7.7') {
                   entry 'slf4j-api'
                   entry 'slf4j-simple'
              }
         }
    }

and then I could use dependency without version

dependencies {
    compile 'org.springframework:spring-core'
}

How can I get the same behavior in naive Gradle? I mean: I'd like to define a version once as I did it when using Dependency Management Plugin

like image 416
wakedeer Avatar asked Oct 28 '25 05:10

wakedeer


1 Answers

Solution below helps to avoid versions copy-paste. However it isn't the same with Dependency Management plugin.

For Gradle Kotlin Dsl:

You can create buildSrc with you own code, when you can place any constants.

Algorithm:

  1. Create folder buildSrc/src/main/kotlin
  2. Create file buildSrc/src/main/kotlin/Versions.kt with content:
object Versions {
    const val junitVersion = "5.5.5" // just example
}
  1. Create file buildSrc/build.gradle.kts with content:
plugins {
    `kotlin-dsl`
}
  1. Use the following syntax in your gradle.kts files:
dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter:${Versions.junitVersion}")
}

For Gradle Groovy:

  1. Create file gradle.properties
  2. Put versions there with syntax like okhttp_version=4.2.0
  3. Use the following syntax in your gradle files:
dependencies {
    compile group: 'com.squareup.okhttp3', name: 'okhttp', version: okhttp_version
}
like image 90
Manushin Igor Avatar answered Oct 31 '25 07:10

Manushin Igor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!