2

我正在尝试在 kotlin 中创建模块化构建脚本。基本上是主脚本和依赖脚本。在 build.gradle.kts 我有:

applyFrom("dependencies.kts")

在 dependencies.kts 我有实际的依赖项:

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) }
}

这失败了:

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

如果我尝试导入 kotlinModule,则会失败:

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

我做错了什么以及如何做对?

版本及相关信息:

  • 梯度:4.0
  • 摇篮 KTS:0.9.0
  • 编辑器:IntelliJ U 2017.1.4
  • Kotlin 插件:1.1.3 EAP
  • 项目的 Kotlin 版本:1.1.2.5

当我在主文件中有依赖项时,我的构建工作完美。所有必要的配置(构建脚本、插件、存储库等)都存在,但为简洁起见,此处省略。

4

1 回答 1

2

这里有几件事:

  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.

于 2017-06-20T12:17:56.557 回答