Gradle平台依赖与java-library

huangapple go评论80阅读模式
英文:

Gradle platform dependencies with java-library

问题

以下是翻译好的部分:

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

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

错误信息:

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)

如果您需要更多帮助,请随时提问。

英文:

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...

答案1

得分: 1

我正在使用 Gradle v7.4,并且通过在我的 build.gradle 中添加以下内容来解决了这个问题:

java {
  disableAutoTargetJvm()
}

我在 将 Gradle 版本从 5.x 升级到 6.0 的笔记中找到了答案。

英文:

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.

huangapple
  • 本文由 发表于 2020年9月5日 22:58:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63755291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定