简体   繁体   中英

Gradle platform dependencies with java-library

We have a couple of common libraries, which use the java-library Gradle plugin, and depend on Micronaut via a platform dependency on it's BOM. Both projects use Gradle 6.6.1 which is the latest at the time of writing this post.

Library build.gradle

plugins {
    id "java-library"
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {

    annotationProcessor (
        platform("io.micronaut:micronaut-bom:$micronautVersion"),
        "io.micronaut:micronaut-inject-java"
    )

    implementation(
        platform("io.micronaut:micronaut-bom:$micronautVersion"),
        "io.micronaut:micronaut-inject"
    )

    testAnnotationProcessor(
        enforcedPlatform("io.micronaut:micronaut-bom:$micronautVersion"),
        "io.micronaut:micronaut-inject-java"
    )

    testImplementation(
        enforcedPlatform("io.micronaut:micronaut-bom:$micronautVersion"),
        "io.micronaut:micronaut-runtime",
        "io.micronaut:micronaut-http-server-netty",
        "io.micronaut:micronaut-http-client"
    )
}

When I try to use my library in an application, which uses the application Gradle plugin and also has a platform dependency on Micronaut's BOM, I get an error saying it can't resolve the Micronaut BOM the library uses. But it will never be able to resolve that dependency because it's a BOM, not a library.

Application build.gradle

plugins {
    id "application"
    id "java"
}

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation(
        "my.group:library:${libraryVersion}",
    
        platform("io.micronaut:micronaut-bom:${micronautVersion}"),
        "io.micronaut:micronaut-http-client",
        "io.micronaut:micronaut-http-server-netty",
        "io.micronaut:micronaut-inject",
        "io.micronaut:micronaut-management",
        "io.micronaut:micronaut-runtime",
        "io.micronaut:micronaut-validation"
    )
}


And then I get the following error.

Caused by: org.gradle.internal.resolve.ModuleVersionResolveException: Could not resolve io.micronaut:micronaut-bom:2.0.1.
Required by:
    project : > my.group:library:11.0.0-SNAPSHOT
Caused by: org.gradle.internal.component.NoMatchingConfigurationSelectionException: No matching variant of io.micronaut:micronaut-bom:2.0.1 was found. The consumer was configured to find a runtime of a library compatible with Java 14, packaged as a jar, and its dependencies declared externally but:
  - Variant 'apiElements' capability io.micronaut:micronaut-bom:2.0.1:
      - Incompatible because this component declares an API of a platform and the consumer needed a runtime of a library
      - Other compatible attributes:
          - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
          - Doesn't say anything about its target Java version (required compatibility with Java 14)
          - Doesn't say anything about its elements (required them packaged as a jar)

I've attempted to read the Gradle docs regarding variant selection, but it really doesn't make much sense to me. Anyone know how I need to declare the dependencies to make this work? It seems like a pretty trivial thing, but I haven't found anything about this...

I'm using Gradle v7.4 and was able to resolve the problem by adding this to my build.gradle :

java {
  disableAutoTargetJvm()
}

I found the answer in the Upgrading Gradle version 5.x to 6.0 notes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM