2017-06-20 257 views
1

我想在kotlin中创建模块化构建脚本。基本上主要的脚本和依赖脚本。在build.gradle.kts我: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 2017年1月4日
  • 科特林插件:1.1.3 EAP
  • Kotlin版本的项目:1.1.2.5

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

回答

1

几件事情在这里:

  1. 我假设你有内部build.gradle.ktsapplyFrom("dependencies.kts")什么;如果是的话,你仍然需要就可以了buildscriptplugins块:

    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应改名为dependencies.gradle.kts(也参考)

  3. 根据所使用kotlinModule("stdlib-jre8")的摇篮版本可能被弃用已经;最近使用了kotlin("stdlib-jre8")
  4. 您错过了该文件本身的一些其他设置(buildscript,repositories和潜在的plugins也)。

    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.

有在Kotlin language support for Gradle build scripts的一些例子看一下;他们在这里做的事情略有不同,但你可能有这样的要求。

+0

嗨。感谢您的详细回复。我在'build.gradle.kts'中有buildscript,插件,存储库,插件配置等,当依赖关系阻塞在文件中时它就像一个魅力一样。我在gradle 4.0(KTS 0.9.0)上。文件的重命名部分工作,我现在得到不同的异常:'错误:没有找到名称'编译'配置' – Czar

+0

顺便说一句,我也在启动2.0.0.M2 :)和Kotlin 1.1。 2-5 – Czar

+0

...这就是为什么我还说你还需要'buildscript',''repositories'和潜在的'plugins'也是 –