Is there any way to list latest Nexus Artifacts in Jenkins?
Asked Answered
L

2

2

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

Lorenzetti answered 24/6, 2020 at 4:49 Comment(0)
A
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()
Asyut answered 14/11, 2020 at 6:25 Comment(0)
S
1

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)
Szechwan answered 25/1, 2023 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.