Gradle构建失败:在:compileJava处出现`No matching variant`。

5

我正在尝试为Minecraft Bukkit(Paper)插件编写一个库。 我试图构建一个jar文件,但Gradle给我这个错误:

Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.

我以前从未见过这个错误。

Execution failed for task ':compileJava'.
> Could not resolve all files for configuration ':compileClasspath'.
   > Could not resolve io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT.
     Required by:
         project :
      > No matching variant of io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT:20220703.182221-166 was found. The consumer was configured to find an API of a library compatible with Java 16, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally but:
          - Variant 'apiElements' capability io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT declares an API of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 16
              - Other compatible attribute:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
          - Variant 'javadocElements' capability io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT declares a runtime of a component, 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 16)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
          - Variant 'runtimeElements' capability io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT declares a runtime of a library, packaged as a jar, and its dependencies declared externally:
              - Incompatible because this component declares a component compatible with Java 17 and the consumer needed a component compatible with Java 16
              - Other compatible attribute:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
          - Variant 'sourcesElements' capability io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT declares a runtime of a component, 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 16)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)


完整的输出日志太长了,我已经将它上传到 pastebin: 使用-scan -debug(完整输出)
plugins {
    id("java")
    id("maven-publish")
}

group = "ml.windleaf"
version = "1.0.1"

repositories {
    mavenCentral()
    maven("https://jitpack.io")
    maven("https://repo.papermc.io/repository/maven-public/")
}

dependencies {
    testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
    implementation("io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT")
    // https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
    implementation("org.apache.logging.log4j:log4j-core:2.18.0")
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}

allprojects {
    apply(plugin = "java")
    apply(plugin = "java-library")
    apply(plugin = "maven-publish")

    repositories {
        mavenCentral()
        maven("https://jitpack.io")
        maven("https://repo.papermc.io/repository/maven-public/")
    }

    dependencies {
        implementation("org.jetbrains:annotations:23.0.0")
        implementation("org.apache.maven:maven-artifact:3.8.5")
    }

    tasks {
        compileJava {
            options.encoding = "UTF-8"
        }
    }
}

publishing {
    publications {
        create("maven_public", MavenPublication::class) {
            groupId = "ml.windleaf"
            artifactId = "PlugApi"
            version = version
            from(components.getByName("java"))
        }
    }
}

请有人帮忙分析一下为什么会出现这样的错误。


1
我猜可能是Java版本和库不匹配,但我以前就是这样使用的,没有出现错误。 - WindLeaf
1个回答

9
tl;dr: Paper API文档指明需要将Java版本设置为17,而你似乎没有这样做。尝试添加。
java {
    toolchain.languageVersion.set(JavaLanguageVersion.of(17))
}

解释

Gradle 给出的错误信息过于冗长,但它确实解释了发生了什么。让我们来逐步分析一下。

最值得关注的是以下部分:

Could not resolve io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT

Gradle找不到你声明的依赖项。从查看Paper API文档来看,你使用了正确的Maven Repository和正确的依赖项,因此,Gradle必须有其他原因...
No matching variant of 
io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT:20220703.182221-166 
was found.

The consumer was configured to find an API of a library compatible with Java 16

基本上,“消费者”(即您的项目)正在说:“我需要与Java 16兼容的东西,因为这是我正在使用的版本。”
Incompatible because this component declares a component compatible with Java 17
and the consumer needed a component compatible with Java 16

现在Gradle找到了一些非常接近的东西,但是它被淘汰了,因为它使用的是Java 17,这与您的项目所需不符。

Gradle还列出了一些其他变体,但它们也被淘汰了,因为它们不是Java库——它们是源代码或Javadoc变体。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接