Extracting dependency coordinates from build.gradle.kts

(Ross Goldberg) #1

I have a Kotlin DSL build that is working. I want to move my dependency coordinates from my build.gradle.kts to another file so that I can include the same file in multiple build.gradle.kts files.

I currently have something like the following in my build.gradle.kts:

ext {
			"guava" to mapOf("group" to "com.google.guava", "name" to "guava", "version" to "25.1-jre")

dependencies {
	val libraries = ext.get("libraries") as Map<String, Map<String, String>>

I tried moving the ext block to a file named libraries.gradle.kts, and inserting the following before my dependencies block in build.gradle.kts:

apply(from = "libraries.gradle.kts")

But that gives the following error:

* Where:
Script 'libraries.gradle.kts' line: 1

* What went wrong:
Script compilation errors:

  Line 1: ext {
          ^ Unresolved reference: ext

  Line 5:       set(
           ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
               @InlineOnly public inline operator fun <K, V> MutableMap<String, Map<String, Map<String, String>>>.set(key: String, value: Map<String, Map<String, String>>): Unit defined in kotlin.collections
               @InlineOnly public inline operator fun kotlin.text.StringBuilder /* = java.lang.StringBuilder */.set(index: Int, value: Char): Unit defined in kotlin.text

2 errors

How can I extract my dependency coordinate map into another file, and reference that file from build.gradle.kts? I’d prefer the external file to be static, e.g., using the Kotlin DSL instead of the Groovy DSL.