我的构建文件看起来像这样:
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:字符串'在此上下文中无法通过隐式接收器调用。如有必要,请使用显式
我该如何修复?
您可以在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
我一直在围绕项目内部和外部定义构建构建块的属性进行一些测试,但似乎不起作用。如果要访问依赖关系相关的属性,则工作和推荐的解决方案是定义buildsrc模块(检查此Github repo),然后添加带有所需属性的Kotlin文件/类(版本,字符串...)。类似:
object Dependency {
object Foundation {
const val KOTLIN = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.Global.KOTLIN}"
const val RANDOM_VERSION = "1.2.3"
}
您可以访问buildsrc模块中声明的任何内容,因为如果我没记错的话,在初始化阶段中声明的组件已解决。用法示例如下:
plugins {
val kotlin = Dependency.Foundation.KOTLIN
kotlin("kapt").version(Dependency.Foundation.RANDOM_VERSION)
}
更多信息。
upd 不起作用,对不起。
原始答案:
-
另一个解决方法是使用const val
编译时间常数:
object Defs {
const val kotlinVersion = "1.2.60"
}
buildscript {
val kotlinVersion:String by extra { Defs.kotlinVersion }
}
plugins {
kotlin("jvm") version Defs.kotlinVersion
}