Search code examples
apigroovynexussonatypenexus3

Sonatype Nexus search api does not return all the artifacts


We use sonatype nexus OSS 3.14.0-04 to store all our artifacts and we use Jenkins as our CI/CD took. I introduced snapshot support recently. From Jenkins I use the below groovy script to fetch all artifact versions and populate an Active choice drop-down:

#!/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()

Solution

  • The nexus api search endpoint uses a pagination strategy to return the result. If the global result contains several pages, the current result will contain a continuationToken you can use in your next query to fetch the next result page.

    If your search returns more results than the page size, you will need to make several calls to the search url to get all the results in your Jenkins script (i.e. until continuationToken is null).

    Ref: Nexus Search API endpoint doc (also referenced in your swagger-ui api view in your nexus admin)