【问题标题】:Sonatype Nexus search api does not return all the artifactsSonatype Nexus 搜索 api 不返回所有工件
【发布时间】:2020-03-29 23:05:52
【问题描述】:

我们使用 sonatype nexus OSS 3.14.0-04 来存储我们所有的工件,并使用 Jenkins 作为我们的 CI/CD 所用。我最近介绍了快照支持。在 Jenkins 中,我使用以下 groovy 脚本来获取所有工件版本并填充活动选择下拉列表:

#!/usr/bin/env  groovy
import groovy.json.JsonSlurper

def source = "release"
def url = "http://<<nexus-host>>/service/rest/v1/search?repository=maven-releases&maven.groupId=com.xyz&maven.artifactId=woof&maven.extension=war"
Set<String> versions = new HashSet<>();
def continuationToken = null
def count = 0

def url1 = url
while (true) {
    def xml = url1.toURL().text
    JsonSlurper parser = new groovy.json.JsonSlurper()
    Map parsedJson = parser.parseText(xml)
    def rawVer = parsedJson.items.version
    continuationToken = parsedJson.continuationToken

    if (continuationToken != null) {
        url1 = url + "&continuationToken=" + continuationToken
    }

    if (source.equalsIgnoreCase('snapshot')) {

        Set<String> modVersion = new HashSet<>()
        for (def item : rawVer) {
            modVersion.add(item.split("-").getAt(0) + "-SNAPSHOT")
        }

        versions.addAll(modVersion)
    } else {
        versions.addAll(rawVer)
    }

    if (continuationToken == null || count > 5) {
        break;
    }
}

return versions.sort().reverse()

【问题讨论】:

    标签: api groovy nexus sonatype nexus3


    【解决方案1】:

    nexus api 搜索端点使用分页策略返回结果。如果全局结果包含多个页面,则当前结果将包含 continuationToken,您可以在下一个查询中使用它来获取下一个结果页面。

    如果您的搜索返回的结果多于页面大小,您将需要多次调用搜索 url 以获取 Jenkins 脚本中的所有结果(即直到 continuationToken 为空)。

    参考:Nexus Search API endpoint doc(也在您的 nexus 管理员的 swagger-ui api 视图中引用)

    【讨论】:

    • 那行得通。谢谢,为此,我应该自己去拿。
    • continuationToken 仅适用于默认 Nexus 中的 200 个页面,因为 index.max_result_window=10000 设置,并且 Nexus 每页显示 50 个项目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 2013-06-26
    • 2013-09-11
    • 2015-02-14
    • 2020-08-30
    • 2016-02-19
    • 2016-08-29
    相关资源
    最近更新 更多