4

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.

mkobit
  • 43,979
  • 12
  • 156
  • 150
Czar
  • 1,633
  • 2
  • 17
  • 32

1 Answers1

3

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.

x80486
  • 6,627
  • 5
  • 52
  • 111
  • Hi. Thanks for thorough reply. I do have buildscript, plugins, repositories, plugins configuration, etc. in `build.gradle.kts` and it works like a charm when dependencies block is in said file. I'm on gradle 4.0 (KTS 0.9.0). The renaming of the file worked in part, I now get different exception: `Error:Configuration with name 'compile' not found.` – Czar Jun 20 '17 at 13:25
  • And by the way I'm also on Boot 2.0.0.M2 :) and Kotlin 1.1.2-5 – Czar Jun 20 '17 at 13:32
  • ...that's why I was saying also you need the `buildscript`, `repositories` and potentially `plugins` also – x80486 Jun 20 '17 at 13:32
  • yes, I have them, but omitted for brevity, because they work, the only problem I have is the one described. – Czar Jun 20 '17 at 13:35
  • And I'm always online on Kotlin Slack when I'm at the PC :) – Czar Jun 20 '17 at 13:38
  • https://kotlinlang.slack.com/archives/C19FD9681/p1497950969066738 ← this is my share of this question in there – Czar Jun 20 '17 at 13:40
  • 1
    Just realized, renaming the file actually worked, I was misplacing the applyFrom. Thanks :) – Czar Jun 20 '17 at 13:50