gpt4 book ai didi

Gradle 预编译脚本插件失败,第一个 block 为 `expression ... cannot be invoked as a function`

转载 作者:行者123 更新时间:2023-12-03 03:05:52 32 4
gpt4 key购买 nike

我有以下 预编译脚本插件 ,它应用了一个 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.ktsbuildSrc :
// 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 无关插件。

我需要更改什么才能使我的插件工作?

最佳答案

问题是在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"

关于Gradle 预编译脚本插件失败,第一个 block 为 `expression ... cannot be invoked as a function`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58826817/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com