Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle Script Kotlin and dependencyManagement

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"
    }
}
like image 217
wst Avatar asked Nov 26 '16 13:11

wst


People also ask

What is Dependencymanagement in Gradle?

In most cases, a project relies on reusable functionality in the form of libraries or is broken up into individual components to compose a modularized system. Dependency management is a technique for declaring, resolving and using dependencies required by the project in an automated fashion.

What is dependencies in Kotlin?

Dependency types These are multiplatform libraries that support multiple targets and can be used in the common source set, commonMain . Many modern Android libraries already have multiplatform support, like Koin, Apollo, and Okio.

How does Gradle resolve transitive dependencies?

Releases of a module hosted on a repository can provide metadata to declare those transitive dependencies. By default, Gradle resolves transitive dependencies automatically. The version selection for transitive dependencies can be influenced by declaring dependency constraints.

What is Maven BOM in Gradle?

Maven's dependency management includes the concept of a bill-of-materials (bom). A bom is a special kind of pom that is used to control the versions of a project's dependencies and provides a central place to define and update those versions.


1 Answers

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")
    }
}
like image 74
James Bassett Avatar answered Sep 26 '22 19:09

James Bassett