我想将我的项目拆分为多个子项目。 IntelliJ IDE的默认Gradle设置为:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
该设置会编译。但是我不想在每个子项目中重复该代码。所以我将build.gradle.kts更改为import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
subprojects {
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
但我得到一个异常(exception):e: C:[...]\build.gradle.kts:1:12: Unresolved reference: jetbrains e: C:[...]\build.gradle.kts:16:9: Unresolved reference: implementation e: C:[...]\build.gradle.kts:19:20: Unresolved reference: KotlinCompile e: C:[...]\build.gradle.kts:19:35: Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected e: C:[...]\build.gradle.kts:20:9: Unresolved reference: kotlinOptions
FAILURE: Build failed with an exception.
Where: Build file 'C:[...]\build.gradle.kts' line: 1
What went wrong: Script compilation errors:
Line 01: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile ^ Unresolved reference: jetbrains
Line 16: implementation(kotlin("stdlib-jdk8")) ^ Unresolved reference: implementation
Line 19: tasks.withType { ^ Unresolved reference: KotlinCompile
Line 19: tasks.withType { ^ Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
Line 20: kotlinOptions.jvmTarget = "1.8" ^ Unresolved reference: kotlinOptions
5 errors
Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 1s
我认为有一个简单的语法错误,但我找不到。
最佳答案
不知道如何通过将plugins { }
嵌套在subprojects { }
下来也不会出错,如Limitations of the plugins DSL中所述:
The
plugins {}
block must also be a top level statement in the buildscript. It cannot be nested inside another construct (e.g. an if-statement or for-loop).
因此,要解决您的问题,请将
plugins {}
移到顶部,并强制在subprojects {}
块中应用插件:import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50" apply false
}
subprojects {
apply {
plugin("org.jetbrains.kotlin.jvm")
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
val implementation by configurations
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
您可以在
apply false
块的类型/范围PluginDependenciesSpec
的Method details中阅读有关plugins {}
部分的更多信息。您可以在Understanding what to do when type-safe model accessors are not available中阅读有关
val implementation by configurations
部分的更多信息
关于gradle - 通过使用kotlin-gradle中的子项目"Unresolved reference: implementation",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58669082/