Search code examples
jenkinscurlrepositorynexusnexus3

How to list all component in Nexus repository with API?


I use RAW format repository in my Nexus called myrepo. I upload files such as .tar.gz, zip, exe ...etc. In this repository I've many sub-folders and files and now I want to list all of the files with API.

I use this execution, generated by Nexus UI:

curl -X GET "http://localhost:8081/service/rest/v1/search?repository=myrepo&format=raw" -H  "accept: application/json"

The problem is that results are not full. The result are around 1000 lines of json, but there are another files which are missing from the results.

I've also try to filter by name:

curl -X GET "http://localhost:8081/service/rest/v1/search?q=update&repository=myrepo&format=raw" -H  "accept: application/json"

but is the same - The list is not complete.

My question is:

How can I list all of the components in this RAW repository?


Solution

  • I manage to do this with groovy script

    In the script I get the token and pass it in loop like this:

    import groovy.json.JsonSlurper
    
    def nexusURL = "http://localhost:8081/service/rest/beta/search?repository=myrepo&format=raw"
    
    // Make request to Nexus API and get continuationToken
    def nexusAPIResponse = new URL(nexusURL).text;
    def nexusAPISlurper = new JsonSlurper()
    def nexusAPIResponseSlurper = nexusAPISlurper.parseText(nexusAPIResponse)
    def continuationToken = nexusAPIResponseSlurper.continuationToken
      println "continuationToken: "+continuationToken
      println 'nexusAPIResponseSlurper: '+nexusAPIResponseSlurper.items.name.size()
      println "--------------------------------"
    
      try {
        while(continuationToken != 'null'){
        // Make request to Nexus API with continuationToken
        def nexusAPIResponseWithToken = new URL("${nexusURL}&continuationToken=${continuationToken}").text;
        def nexusAPISlurperWithToken = new JsonSlurper()
        def nexusAPIResponseSlurperWithToken = nexusAPISlurperWithToken.parseText(nexusAPIResponseWithToken)
          continuationToken = nexusAPIResponseSlurperWithToken.continuationToken
          nexusAPIResponseSlurperWithToken.items.name.each {
            println it
          }
        }
      }
      catch(IOException ex) {
        println "--------------------------------"
      }