简体   繁体   English

如何使用 API 列出 Nexus 存储库中的所有组件?

[英]How to list all component in Nexus repository with API?

I use RAW format repository in my Nexus called myrepo .我在我的 Nexus 中使用名为myrepo RAW格式存储库。 I upload files such as .tar.gz , zip , exe ...etc.我上传文件,如.tar.gzzipexe ...等。 In this repository I've many sub-folders and files and now I want to list all of the files with API .在这个存储库中,我有很多子文件夹和文件,现在我想用API列出所有文件。

I use this execution, generated by Nexus UI:我使用由 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.结果是大约 1000 行 json,但结果中还缺少另一个文件。

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?如何列出此 RAW 存储库中的所有组件?

I manage to do this with groovy script我设法用groovy脚本做到这一点

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 "--------------------------------"
  }

Use the integrated Pagination使用集成分页

https://help.sonatype.com/repomanager3/rest-and-integration-api/pagination https://help.sonatype.com/repomanager3/rest-and-integration-api/pagination

you GET the first page of result, then check for continuationToken你 GET 结果的第一页,然后检查continuationToken

{
  "items" : [
    ...
  ],
  "continuationToken" : "88491cd1d185dd136f143f20c4e7d50c"
}

if it's not null there's more to GET simply adding:如果它不为空,则只需添加更多内容即可:

&continuationToken= continuationToken &continuationToken= continuationToken

to your get, Groovy with JsonSlurper is great way to do it根据您的需要,带有 JsonSlurper 的 Groovy 是实现此目的的好方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM