如何手动调用Gradle的存储库搜索?

时间:2017-03-12 11:43:10

标签: gradle

作为构建发布版本的先决条件,比如acme-widgets-1.0,我想搜索已配置的存储库(或特定存储库)以确保acme-widgets-1.0.jar尚未发布。我怎么能用Gradle做到这一点?或者,有没有办法为特定版本配置不存在的依赖项?

group = 'com.acme'
version = '1.0'

repositories {
    jcenter()
}

if (gradle.startParameter.taskNames.contains("release")) {
    def taskNames = gradle.startParameter.taskNames
    taskNames.add(0, "checkReleaseDoesntExist")
    gradle.startParameter.taskNames = taskNames
}

task checkReleaseDoesntExist() {
}

task release() {
    println "Building release"
}

checkReleaseDoesntExist.doLast {
    println "Checking repositories to make sure release hasn't already been built"
    // TODO What do I do here?
}

1 个答案:

答案 0 :(得分:1)

您可以声明一个配置,其中只包含您尝试查看的工件(如果它们已经发布)。 解析此配置后,您将拥有一个空文件集。

这就是:

configurations {
    checkRelease { transitive = false }
}
dependency {
    checkRelease "$group:$name:$version"
}

task ('checkReleaseDoesntExist') {
    doLast {
        println "Checking repositories to make sure release hasn't already been built"
        try {
            if (!configurations.checkRelease.files.isEmpty()) {
                // already exists
            }
        }
        catch (ResolveException e) {
            // doesn't exist
        }
}