You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle中splain插件Java兼容性不符?如何禁用消费端检查?

问题描述

我正尝试为Scala 2.13.11使用splain插件1.0.2。根据其构建定义文件,它被声明为兼容Java 1.8及以上版本:

tasks {
    val jvmTarget = JavaVersion.VERSION_1_8.toString()

    withType<ScalaCompile> {
        targetCompatibility = jvmTarget
    }
}

但将其作为Scala编译器插件引入时:

val splainD = "io.tryp:splain_2.13.11:1.0.2"
scalaCompilerPlugins(splainD)

我遇到了如下错误:

Execution failed for task ':prover-commons:core:compileTestFixturesScala'.

Could not resolve all files for configuration ':prover-commons:core:testFixturesCompileClasspath'.

Could not resolve io.tryp:splain_2.13.11:1.0.2.

Required by:
project :prover-commons:core

No matching variant of io.tryp:splain_2.13.11:1.0.2 was found. The consumer was configured to find a library for use during compile-time, compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally but:

  • Variant 'apiElements' capability io.tryp:splain_2.13.11:1.0.2 declares a library for use during compile-time, packaged as a jar, and its dependencies declared externally:
  • Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
  • Other compatible attribute:
  • Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
  • Variant 'javadocElements' capability io.tryp:splain_2.13.11:1.0.2 declares a component for use during runtime, and its dependencies declared externally:
  • Incompatible because this component declares documentation and the consumer needed a library
  • Other compatible attributes:
  • Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
  • Doesn't say anything about its target Java version (required compatibility with Java 8)
  • Doesn't say anything about its elements (required them preferably in the form of class files)
  • Variant 'runtimeElements' capability io.tryp:splain_2.13.11:1.0.2 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
  • Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
  • Other compatible attribute:
  • Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
  • Variant 'sourcesElements' capability io.tryp:splain_2.13.11:1.0.2 declares a component for use during runtime, and its dependencies declared externally:
  • Incompatible because this component declares documentation and the consumer needed a library
  • Other compatible attributes:
  • Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
  • Doesn't say anything about its target Java version (required compatibility with Java 8)
  • Doesn't say anything about its elements (required them preferably in the form of class files)

我的问题是:

  1. 为何splain 1.0.2被报告仅兼容Java 11,尽管已显式指定使用其他Java版本?
  2. 如何从消费端临时禁用该兼容性检查?

解答

1. 兼容性报告为Java 11的原因

仅设置ScalaCompile的targetCompatibility不足以让Gradle正确识别组件的Java兼容性属性。这个配置只是控制编译时生成的字节码版本,但Gradle发布组件时,还需要通过JavaPluginExtension的toolchain或者显式配置java组件的属性来声明兼容的Java版本。

从错误信息推断,splain的发布包中,Gradle自动推断出的兼容Java版本是11——这很可能是因为构建该插件时使用的JDK版本是11。Gradle在没有显式配置兼容性属性的情况下,会默认使用构建环境的JDK版本作为组件的兼容版本,忽略targetCompatibility的设置。

2. 消费端临时禁用兼容性检查

可以通过修改Gradle配置,针对splain插件的依赖调整兼容性规则:

方法1:针对特定依赖修正属性

在项目的build.gradle.kts中添加如下配置,强制将splain的目标JVM版本设置为8:

configurations.all {
    resolutionStrategy.eachDependency {
        if (requested.group == "io.tryp" && requested.name == "splain_2.13.11" && requested.version == "1.0.2") {
            attributes {
                attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
            }
        }
    }
}

方法2:全局放宽检查(仅临时使用)

如果需要临时绕过所有Java版本检查,可以添加:

configurations.all {
    attributes.attribute(TargetJvmVersion.TARGET_JVM_VERSION_ATTRIBUTE, 8)
}

或者直接指定依赖的能力选择:

configurations.all {
    resolutionStrategy.capabilitiesResolution.withCapability("io.tryp:splain_2.13.11") {
        select("io.tryp:splain_2.13.11:1.0.2")
    }
}

注意:全局禁用兼容性检查会带来潜在的运行风险,仅建议在临时调试时使用,长期来看最好推动插件官方修复其发布配置,正确声明兼容的Java版本。


内容的提问来源于stack exchange,提问作者tribbloid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:30:53