Gradle预编译脚本插件因“表达式...失败...不能作为函数调用”的第一个块

我有以下预编译脚本插件,它应用了Gradle核心插件和外部插件(通过id(...)

// buildSrc/main/kotlin/my-template.gradle.kts:
import org.gradle.api.JavaVersion

plugins {
    java
    id("com.diffplug.gradle.spotless") // commenting this line "fixes" the problem,WHY?
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
}

build.gradle.kts中有这个buildSrc

// buildSrc/build.gradle.kts:
repositories {
    maven("https://nexus.ergon.ch/repository/secure-public/")
}

plugins {
    `kotlin-dsl`
    id("com.diffplug.gradle.spotless") version "3.25.0"
}

构建失败,并显示以下消息:Expression 'java' cannot be invoked as a function. The function 'invoke()' is not found

$ ./gradlew tasks

> Task :buildSrc:compileKotlin FAILED
The `kotlin-dsl` plugin applied to project ':buildSrc' enables experimental Kotlin compiler features. For more information see https://docs.gradle.org/5.6.4/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin
e: .../buildSrc/src/main/kotlin/my-template.gradle.kts: (8,1): Expression 'java' cannot be invoked as a function. The function 'invoke()' is not found
e: .../buildSrc/src/main/kotlin/my-template.gradle.kts: (8,1): Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: 
internal val OrgGradlePluginGroup.java: PluginDependencySpec defined in gradle.kotlin.dsl.plugins._279e7abc24718821845464f1e006d45a in file PluginSpecBuilders.kt
public val <T> KClass<TypeVariable(T)>.java: Class<TypeVariable(T)> defined in kotlin.jvm
public val PluginDependenciesspec.java: PluginDependencySpec defined in org.gradle.kotlin.dsl
e: .../buildSrc/src/main/kotlin/my-template.gradle.kts: (9,5): Unresolved reference: sourceCompatibility


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':buildSrc:compileKotlin'.
> Compilation error. See log for more details

我正在使用Gradle 5.6.4,自Gradle 5.3起,预编译的脚本插件应该能够利用类型安全的访问器。

(此外,java {}块在IntelliJ中以红色突出显示,并且没有代码完成)

只要在plugins {}块中列出了任何外部插件,就会出现此问题,它与特定的spotless插件无关。

问题似乎总是影响plugins {}块之后的第一个块,因此它似乎也与特定的java插件无关。 >

要使我的插件正常工作,我需要更改什么?

lihuizi0806 回答:Gradle预编译脚本插件因“表达式...失败...不能作为函数调用”的第一个块

问题在于,在buildSrc/build.gradle.kts中使用了外部Gradle插件id("com.diffplug.gradle.spotless")(通过plugins {}块),但是没有声明依赖项(通过dependencies块) )在提供插件的工件上:

plugins {
    `kotlin-dsl`
    // use "apply false" to specify the exact version (which is
    // forbidden in the pre-compiled script plugin itself) without applying the plugin
    id("com.diffplug.gradle.spotless") version "3.25.0" apply false 
}

dependencies {
    // actually depend on the plugin to make it available:
    implementation(plugin("com.diffplug.gradle.spotless",version = "3.25.0")) 
}

// just a helper to get a syntax similar to the plugins {} block:
fun plugin(id: String,version: String) = "$id:$id.gradle.plugin:$version" 
本文链接:https://www.f2er.com/3114230.html

大家都在问