18

I am trying to port Spring Cloud Stream app build script to Kotlin. So far, so good, except the dependency management block. It's difficult to find anything in the net. Samples don't cover that topic, too.

How do I convert following block to build.gradle.kts? Thanks.

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR2"
    }
}
James Bassett
  • 9,458
  • 4
  • 35
  • 68
wst
  • 4,040
  • 4
  • 41
  • 59

3 Answers3

16

Totally not tested, but I believe it should be something like this:

import io.spring.gradle.dependencymanagement.DependencyManagementExtension
import io.spring.gradle.dependencymanagement.ImportsHandler

configure<DependencyManagementExtension> {
    imports(delegateClosureOf<ImportsHandler> {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:Camden.SR2")
    })
}

If you haven't seen it, you should be familiar with gradle script kotlin's project extensions and groovy interop functions. You really have to dig into the source of the groovy plugin you're configuring to see how it expects to use the closure. The examples in the gradle script kotlin project are also a good guide.

Edit 19 Dec 2016

The latest version of the dependency management plugin is now more gradle script kotlin friendly and will allow the following:

configure<DependencyManagementExtension> {
    imports {
        it.mavenBom("io.spring.platform:platform-bom:Camden.SR2")
    }
}

It could still benefit from some Kotlin extension functions to remove the need for it (using a receiver instead), but definitely an improvement!

Edit 3 Nov 2017

It now works without the it, like so:

configure<DependencyManagementExtension> {
    imports {
        mavenBom("io.spring.platform:platform-bom:Camden.SR2")
    }
}
nerdherd
  • 2,508
  • 2
  • 24
  • 40
James Bassett
  • 9,458
  • 4
  • 35
  • 68
  • Works pretty well for an untested solution. Thank you. ;) Unfortunately I am just Kotlin newbie. But thanks for a tip, I'll take a better look into the code. – wst Nov 28 '16 at 12:29
  • No worries. I'm sure the Spring guides/examples will be updated soon when Gradle Script Kotlin is more mature. Though the better solution would be for Spring to provide some extension functions to make the DSL a bit friendlier (no need for `delegateClosureOf()`). e.g. `fun DependencyManagementExtension.imports(imports: DependencyManagementHandler.() -> Unit){ ...}` – James Bassett Nov 28 '16 at 20:40
9

Gradle supports importing BOM files in the dependency block in both the Groovy and Kotlin DSLs through the platform feature. platform can be used in a few ways, but you will usually see it used as a way to control the versions of transitive dependencies similar to a dependency with <scope>import</scope> in Maven.

The translation of the provided code block would be:

# Kotlin DSL
dependencies {
    // wrap the BOM coordinates with the "platform" keyword
    implementation platform("org.springframework.cloud:spring-cloud-dependencies:Camden.SR2")

    // declare other dependencies
}

Gradle can go a step further to not only suggest the versions of transitive dependencies, but enforce versions as well through enforcedPlatform.

dependencies {
   implementation enforcedPlatform("org.springframework.cloud:spring-cloud-dependencies:Camden.SR2")
}

References: https://docs.gradle.org/current/userguide/dependency_management_terminology.html#sub::terminology_platform https://docs.gradle.org/current/userguide/platforms.html#sub:bom_import

sgilson
  • 91
  • 1
  • 3
8

Using latest io.spring.dependency-management:1.0.6.RELEASE below simpler snippet also works.

plugins {
    id("io.spring.dependency-management") version "1.0.6.RELEASE"
}

dependencyManagement {
    val springCloudVersion = "Finchley.SR2"
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}")
    }
}
Kane
  • 8,035
  • 7
  • 46
  • 75