wakedeer
wakedeer

Reputation: 642

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

Upvotes: 4

Views: 1613

Answers (2)

jaguililla
jaguililla

Reputation: 2086

You can do so on the gradle.properties file. I.e.:

# APPLICATION PROPERTIES
name=projectName
group=com.domain
version=1.0.0
description=A brief description

gradleScripts=https://raw.githubusercontent.com/hexagonkt/hexagon/1.2.0/gradle

# DEPENDENCIES VERSIONS
kotlinVersion=1.3.61
kotlinCoroutinesVersion=1.3.2

Or in settings.gradle if you don't want to create another file:


rootProject.name = "hexagon-contact-application"

gradle.rootProject {

    allprojects {
        version = "1.0.0"
        group = "org.hexagonkt"
        description = "Contact application backend api"
    }

    extensions.gradleScripts = "https://raw.githubusercontent.com/hexagonkt/hexagon/1.0.18/gradle"

    extensions.kotlinVersion = "1.3.50"
    extensions.kotlinCoroutinesVersion = "1.3.2"

    extensions.hexagonVersion = "1.0.21"

    extensions.logbackVersion = "1.2.3"
    extensions.bcryptVersion="0.8.0"
    extensions.javaJwtVersion="3.8.2"
}

And if you want to avoid adding the version variable to all related dependencies, you can create a method in the build file:

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.50'
}

apply from: "$gradleScripts/kotlin.gradle"
apply from: "$gradleScripts/service.gradle"
apply from: "$gradleScripts/junit.gradle"

defaultTasks("test")

mainClassName = 'com.hexagonkt.contact.ApplicationKt'
applicationDefaultJvmArgs = ["-Xms64M", "-Xmx2G", "-XX:+UseNUMA", "-XX:+UseParallelGC"]

dependencies {
    httpkt(it, "http_server_jetty")
    httpkt(it, "store_mongodb")
    httpkt(it, "hexagon_web")

    implementation("at.favre.lib:bcrypt:$bcryptVersion")
    implementation("com.auth0:java-jwt:$javaJwtVersion")

    testImplementation("com.hexagonkt:port_http_client:$hexagonVersion")
}

private void httpkt(final def dependencies, final String artifact) {
    dependencies.implementation("com.hexagonkt:$artifact:$hexagonVersion")
}

Upvotes: 1

Manushin Igor
Manushin Igor

Reputation: 3689

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
}

Upvotes: 2

Related Questions