0

I am using Acive Choice Reactive Parameter plugin to list down Nexus Artifacts. This is the groovy script which I'm currently using.

import groovy.json.*

def targetUrl = "https://nexus.xxxx.lk/service/rest/v1/search?repository=snapshots&format=maven2&group=com.org.pro&name=pro-service"
def jsonSlupper = new JsonSlurper().parse(URI.create(targetUrl).toURL())
def list = jsonSlupper["items"]["version"].collect().sort().reverse()

I want to display only latest artifact in the list. Does anyone know, how to do this?

enter image description here

Janith
  • 217
  • 1
  • 6
  • 15

2 Answers2

3

we can use metadata API, you can use snapshots repo or release repo or public for both, just limit the last 5 versions.

Jenkins snapshot

def host="https://msnexus.xxx.com"
def groupId="com.xxx.cd".replaceAll("\\.", "/")
def artifactId="common-log"
def nexus_url="${host}/repository/public/${groupId}/${artifactId}/maven-metadata.xml"
def response=nexus_url.toURL().text
def metadata = new XmlParser().parseText(response)

metadata.versioning.versions.version.takeRight(5).collect({it.text()}).reverse()
Haber
  • 109
  • 2
  • 4
0

Nexus does support sorting (... and also limits the amount of returned results to 50 by default, see continuationToken).

So to only get the latest versions, adjust your search url to include sort=version&direction=desc, (this does support semver):

def targetUrl = "https://nexus.xxxx.lk/service/rest/v1/search?sort=version&direction=desc&repository=snapshots&format=maven2&group=com.org.pro&name=pro-service"
def jsonSlupper = new JsonSlurper().parse(URI.create(targetUrl).toURL())
def list = jsonSlupper["items"]["version"].collect().take(5)
mbwmd
  • 56
  • 4