Kotlin DSL配置Java规范时发生Gradle编译错误

Pat*_*mer 3 java gradle kotlin gradle-kotlin-dsl

我正在尝试为Kotlin项目创建构建文件,该文件有时会包含Java源代码。过去在多项目构建中使用基于Groovy的构建文件时,我可以毫无问题地sourceCompatibilitysubproject块中指定。使用Kotlin DSL,我知道它必须在java使用Kotlin DSL进行配置的块中,但是当我尝试从subprojectbuild.gradle.kts文件中的块中进行配置时,出现编译错误:

Script compilation errors:

Line 14:     java {
           ^ Expression 'java' cannot be invoked as a function. The function 'invoke()' is not found

Line 14:     java {
           ^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
               public val PluginDependenciesSpec.java: PluginDependencySpec defined in org.gradle.kotlin.dsl

Line 15:         sourceCompatibility = JavaVersion.VERSION_1_8
               ^ Unresolved reference: sourceCompatibility

3 errors
Run Code Online (Sandbox Code Playgroud)

我在要使用的gradle构建文件中包含了要点。现在,如果我在一个子项目build.gradle.kts文件中指定了Java块,就可以使它工作,但是我希望将设置应用于所有子项目,而不仅仅是特定项目。

JB *_*zet 5

你可以用

configure<JavaPluginExtension> { ... }
Run Code Online (Sandbox Code Playgroud)