Search code examples
gradlekotlinbuild.gradle.ktsgradle-kotlin-dsl

Gradle KTS. How to move dependencies configuration to a separate file from main build?


I'm trying to create modular build script in kotlin. Basically main script and dependencies script. in the build.gradle.kts I have:

applyFrom("dependencies.kts")

and in dependencies.kts I have the actual dependencies:

dependencies {
    listOf(
            kotlinModule("stdlib-jre8"),
            // Spring boot
            "org.springframework.boot:spring-boot-starter-web",
            "org.springframework.boot:spring-boot-starter-security",
            "org.springframework.boot:spring-boot-starter-logging",
            "org.springframework.boot:spring-boot-actuator",
            // Spring
            "org.springframework.data:spring-data-mongodb",
            // Logging
            "org.slf4j:slf4j-api",
            "org.slf4j:jcl-over-slf4j",
            "ch.qos.logback:logback-classic"
    ).forEach { compile(it) }

    listOf(
            "org.codehaus.groovy:groovy-all",
            "org.springframework.boot:spring-boot-starter-test",
            "org.spockframework:spock-core:1.0-groovy-2.4",
            "org.spockframework:spock-spring:1.0-groovy-2.4"
    ).forEach { testCompile(it) }
}

This fails with:

Error: Could not find method kotlinModule() for arguments [stdlib-jre8] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

If I try to import kotlinModule, it fails with:

Error:Cause: startup failed:
script '/home/czar/personal/work/***/dependencies.kts': 1: unable to resolve class org.gradle.script.lang.kotlin.kotlinModule
@ line 1, column 1.
import org.gradle.script.lang.kotlin.kotlinModule
^
1 error

What am I doing wrong and how to do it right?

Versions and relevant information:

  • Gradle: 4.0
  • Gradle KTS: 0.9.0
  • editor: IntelliJ U 2017.1.4
  • Kotlin Plugin: 1.1.3 EAP
  • Kotlin version for project: 1.1.2.5

My build works perfectly when I have dependencies in the main file. All necessary configurations (buildscript, plugins, repositories, etc.) are present, but omitted here for brevity.


Solution

  • A couple of things here:

    1. I'm assuming you have nothing inside build.gradle.kts but applyFrom("dependencies.kts"); if so, you still need a buildscript and plugins block on it:

      buildscript {
        repositories {
          //gradleScriptKotlin()
          mavenCentral()
          maven { setUrl("https://repo.spring.io/milestone") }
        }
      
        dependencies {
          classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.M2")
          classpath(kotlin("gradle-plugin"))
        }
      }
      
      plugins {
        //id("io.spring.dependency-management")
        id("org.gradle.application")
        id("org.gradle.idea")
        //id("org.gradle.java")
        id("org.jetbrains.kotlin.jvm") version "1.1.2-5"
        //id("org.springframework.boot")
      }
      ...
      // applyFrom("dependencies.kts")
      
    2. dependencies.kts should be renamed to dependencies.gradle.kts (and also the reference)

    3. Depending on the Gradle version you are using kotlinModule("stdlib-jre8") might be deprecated already; kotlin("stdlib-jre8") is used in the most recent one.
    4. You are missing some more settings on that file itself (buildscript, repositories and potentially plugins also).

      buildscript {
        repositories {
          //gradleScriptKotlin()
          mavenCentral()
          maven { setUrl("https://repo.spring.io/milestone") }
        }
      
        dependencies {
          classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.0.M2")
          classpath(kotlin("gradle-plugin"))
        }
      }
      
      apply {
        plugin("io.spring.dependency-management")
        //plugin("kotlin-jpa")
        //plugin("kotlin-spring")
        plugin("kotlin")
        plugin("org.springframework.boot")
      }
      
      repositories {
        //gradleScriptKotlin()
        mavenCentral()
        maven { setUrl("https://repo.spring.io/milestone") }
      }
      ...
      // ...your `dependencies` block here
      

    Notice I'm using spring-boot-gradle-plugin:2.0.0.M2 and the milestone repo(s); you might be using a stable/prior version. Tweak accordingly.

    Have a look in Kotlin language support for Gradle build scripts for some examples; they are slightly different on what you are doing here, but you might have a requirement on doing things like that.