Gradle 6 个具有严格版本和因为关键字的依赖项

Gradle 6 dependencies with strict version and because keyword

问题

我目前正在尝试使用 Gradle 6.0 和 运行 来解决我想将 because 语句与语法相结合的问题,例如严格和拒绝的版本。

我的构建脚本:

dependencies {
    testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter-api') {
        version {
            strictly '[5.0, 6.0]'
            prefer '5.5.2'
            reject '5.5.1' // for testing purpose only
        }
    }

    testRuntimeOnly(group: 'org.junit.jupiter', name: 'junit-jupiter-engine') {
        version {
            strictly '[5.0, 6.0]'
            prefer '5.5.2'
            reject '5.5.1' // for testing purpose only
        }
    }

    // Force Gradle to load the JUnit Platform Launcher from the module-path
    testRuntimeOnly(group: 'org.junit.platform', name: 'junit-platform-launcher') {
        version {
            strictly '[1.5, 2.0]'
            prefer '1.5.2'
        }
    }
}

到目前为止我尝试了什么

我目前尝试在 version 语句下方或上方添加 because 语句,并在其周围添加花括号,但 none 这些事情似乎都奏效了。

问题

是否可以将 because 语句添加到最后一个依赖项中,如果可以,如何添加? 知道我是否可以将两者 testRuntimeOnly 合二为一也很有趣。

使用 Kotlin DSL,您可以轻松准确地看到 可以使用什么 。因此,将您的示例转换为使用 Kotlin DSL,我们有

dependencies {
    testImplementation("org.junit.jupiter", "junit-jupiter-api") {
        version {
            strictly("[5.0, 6.0]")
            prefer("5.5.2")
            reject("5.5.1") // for testing purpose only
        }
    }

    testRuntimeOnly("org.junit.jupiter", "junit-jupiter-engine") {
        version {
            strictly("[5.0, 6.0]")
            prefer("5.5.2")
            reject("5.5.1") // for testing purpose only
        }
    }

    // Force Gradle to load the JUnit Platform Launcher from the module-path
    testRuntimeOnly("org.junit.platform", "junit-platform-launcher") {
        version {
            strictly("[1.5, 2.0]")
            prefer("1.5.2")
        }
    }
}

Is it possible to add the because statement to the last dependency and if yes, how?

是的。由于我现在使用的是 Kotlin DSL,因此我可以轻松调出智能感知:

你可以在这里看到 because 之外 version 街区可用,所以:

// Force Gradle to load the JUnit Platform Launcher from the module-path
testRuntimeOnly("org.junit.platform", "junit-platform-launcher") {
    version {
        strictly("[1.5, 2.0]")
        prefer("1.5.2")
    }
    because("my reason here.")
}