如何将ext。*variables转换为build.gradle.kts中的插件块

我的构建文件如下所示:

val nexusBaseUri: String by extra val gradle_version: String by extra val kotlin_version: String by extra buildscript { val nexusBaseUri by extra { "https://mynexusserver/nexus" } val gradle_version by extra { "4.1" } val kotlin_version by extra { "1.1.4-3" } val springBoot_version by extra { "2.0.0.M3" } repositories { maven { url = uri("$nexusBaseUri/repository/public") } jcenter() maven { url = uri("http://repo.spring.io/snapshot") } maven { url = uri("http://repo.spring.io/milestone") } } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBoot_version") classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") classpath("org.jetbrains.kotlin:kotlin-allopen:$kotlin_version") } } plugins { application // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv // the following line causes a problem // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv kotlin("jvm", kotlin_version) } apply { plugin("kotlin-spring") plugin("org.springframework.boot") plugin("io.spring.dependency-management") } application { mainClassName = "eqip.fid.FdmlInterpreterDeveloperAppKt" } repositories { maven { url = uri("$nexusBaseUri/content/groups/public") } jcenter() maven { url = uri("http://repo.spring.io/snapshot") } maven { url = uri("http://repo.spring.io/milestone") } } dependencies { compile(kotlin("stdlib")) compile("org.springframework.boot:spring-boot-starter-web") } tasks { "wrapper"(Wrapper::class) { gradleVersion = gradle_version } } 

我在IntelliJ IDEA中得到的错误是

‘val kotlin_version:String’不能在这个上下文中被隐式接收者调用。 如有必要,使用明确的一个

我该如何解决?

您可以在plugins定义一个版本,然后在博客之外访问此版本,例如在dependencies部分。

 plugins { kotlin("jvm").version("1.1.61") } //This is necessary to make the version accessible in other places val kotlinVersion: String? by extra { buildscript.configurations["classpath"] .resolvedConfiguration.firstLevelModuleDependencies .find { it.moduleName == "kotlin-gradle-plugin" }?.moduleVersion } dependencies { compile(kotlin("stdlib", kotlinVersion)) } 

对于版本1.2+,您将不得不用kotlin-gradle-plugin org.jetbrains.kotlin.jvm.gradle.plugin