java - Gradle 构建在 :compileJava: `No matching variant` 处失败

标签 java gradle build.gradle bukkit gradle-kotlin-dsl

我正在尝试为 Minecraft Bukkit(纸)插件创建一个库。我正在尝试构建一个 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了:

With -scan -debug (full output)

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"))
        }
    }
}

请大家帮我分析一下为什么会报这样的错误。

最佳答案

tl;dr:Paper API docs说要将 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 docs您正在使用正确的 Maven 存储库和正确的依赖项,因此 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 变体。

关于java - Gradle 构建在 :compileJava: `No matching variant` 处失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73136123/

相关文章:

使用 Substance LAF,Java 窗口无法在没有任务栏的情况下在屏幕上正确最大化

java - 如何单独格式化双值逗号

java - 在Gradle 4.6版中应用注释处理器

android - 在新的 Android Studio(Bumblebee 及更高版本)Gradle 设置中将 Android Gradle 插件版本放在哪里?

environment-variables - 删除默认的 jdk 1.7 版本并在 windows 上设置 1.6

android - 创建firebase时错误构建gradle

gradle - java.lang.IllegalArgumentException:不支持的类文件主要版本57

gradle - 带有Nexus工件存储库的多项目Gradle构建

gcc - 如何为C项目获取Gradle “cpp”插件以使用gcc而不是g++?

java - 如何获得打开文件弹出窗口