首页 > 解决方案 > 使用 Kotlin DSL 配置 Java 规范的 Gradle 编译错误

问题描述

我正在尝试为有时包含 java 源的 Kotlin 项目创建构建文件。过去在多项目构建中使用基于 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

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

标签: javagradlekotlingradle-kotlin-dsl

解决方案


你可以使用

configure<JavaPluginExtension> { ... }

推荐阅读