Search code examples
javagradlebuild.gradlebukkitgradle-kotlin-dsl

Gradle build failed at :compileJava: `No matching variant`


I'm trying making a library for Minecraft Bukkit (Paper) plugin. I'm trying to build a jar file, but Gradle gives me this error:

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

I'd never seen this error before.

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)

The full output log is so long that I have uploaded it to 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"))
        }
    }
}

Anyone please help me to analyse why it gives such an error.


Solution

  • tl;dr: The Paper API docs say to set the Java version to 17 - it doesn't look like you've done that. Try adding

    java {
        toolchain.languageVersion.set(JavaLanguageVersion.of(17))
    }
    

    Explanation

    The error that Gradle gives you is verbose, but it does explain what's going on. Let's break it down

    The first bit that's interesting is this:

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

    Gradle can't find the dependency that you've declared. From looking at the Paper API docs you're using the right Maven Repository, and the right dependency, so Gradle must have another reason...

    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
    

    Basically, the "the consumer" (that's your project) is saying "I need something that works with Java 16, because that's what I'm using".

    Incompatible because this component declares a component compatible with Java 17
    and the consumer needed a component compatible with Java 16
    

    Now Gradle has found something really close, but it's disqualified because it's using Java 17 - which doesn't match what your project needs.

    Gradle also lists some other variants, but they're also disqualified because they're not a Java library - they're source code, or Javadoc variants.