How to centralize dependencies in Gradle Kotlin?

In groovy script I could assign a list of dependencies in an array as “implementation libraries.frameworkLibs”, does similar approach exists in kotlin?

plugins {
    id 'org.springframework.boot' version '2.1.4.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

sourceCompatibility = '1.8'

/* 1. centerilize dependencies for all projects */
ext.libraries = [
    frameworkLibs: [
        'org.springframework.boot:spring-boot-starter-data-redis',
        'org.springframework.boot:spring-boot-starter-web',
        'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1',
        'org.springframework.kafka:spring-kafka'
    ],
    testLibs: [
        'org.springframework.boot:spring-boot-starter-test',
        'org.springframework.kafka:spring-kafka-test'
    ],
    sqlLibs: [
        'org.postgresql:postgresql'
    ]
]

allprojects {
    group = 'com.example'
    version = '0.0.1-SNAPSHOT'

    repositories {
        mavenCentral()
    }

    dependencies {
        /* 2. establish dependencies */
        implementation libraries.frameworkLibs
        runtimeOnly libraries.sqlLibs
        testImplementation libraries.testLibs
    }
}

Below is what I have in Kotlin. The goal is to have a list with one or more dependencies in a single array as shown in the groovy script above, for example libraries.frameworkLibs, then be able to assign it to dependencies {}.

plugins {
	id("org.springframework.boot") version "2.1.4.RELEASE"
	java

	// this plugin is required for Kotlin
	id("io.spring.dependency-management") version "1.0.7.RELEASE"
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

repositories {
	jcenter()
}

allprojects {
	group = "com.example"
	version = "0.0.1-SNAPSHOT"

	dependencies {
		/* unable to modularize */
		implementation ("org.springframework.boot:spring-boot-starter-data-redis")
		implementation ("org.springframework.boot:spring-boot-starter-web")
		implementation ("org.mybatis.spring.boot:mybatis-spring-boot-starter:2.0.1")
		implementation ("org.springframework.kafka:spring-kafka")
		runtimeOnly ("org.postgresql:postgresql")
		testImplementation ("org.springframework.boot:spring-boot-starter-test")
		testImplementation ("org.springframework.kafka:spring-kafka-test")
	}
}
val version = "1.0"
val anotherVersion= "2.0"

val depArray1 = arrayOf("dep1:$version", "dep3:$version", "dep3:$anotherVersion")
val depArray2 = arrayOf("dep4:$anotherVersion", "dep5$version", "dep6:$anotherVersion")

dependencies {
    depArray1.forEach { implementation(it) }
    depArray2.forEach { implementation(it) }
}

implementation doesn’t have vararg params like in groovy dsl, but kotlin dsl still dsl - you just call function one by another and you can run them in the cycle, also you can move your dependecies holder to buildSrc like this:

1 Like

Reference issue: Dependency declaration DSL should uniformly supports collections of dependency notations · Issue #7649 · gradle/gradle · GitHub