如何在 Gradle 中共享通用测试依赖版本?

How to share common test dependency version in Gradle?

我在 Gradle User Guid 之后使用 kotlin DSL,试图让子项目使用根项目中指定的依赖版本。这是根 build.gradle.kts:

的一部分
plugins {
    `java-platform`
}

dependencies {
    constraints {
        api("com.google.guava:guava:28.0-jre")
        api("org.testng:testng:6.14.3")
    }
}

下面是一个子项目的build.gradle.kts

plugins {
    `java-library`
}

dependencies {
    testImplementation("org.testng:testng")
}

val test by tasks.getting(Test::class) {
    useTestNG()
}

当我在项目根目录运行./gradlew run时,一切正常。然而,当我 运行 ./gradlew test 它抱怨:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':misc:java:compileTestJava'.
> Could not resolve all files for configuration ':misc:java:testCompileClasspath'.
   > Could not find org.testng:testng:.
     Required by:
         project :misc:java

我在网上搜索过,但一无所获。我想我应该在根构建脚本中使用类似 testapi("org.testng:testng:6.14.3") 的东西。

正如评论中已经回答的那样,您在子项目中缺少对平台的引用:

dependencies {
    implementation(platform(rootProject)) // Will use the constraints defined in the platform

    testImplementation("org.testng:testng")
}