Sonatype Nexus 3 - get latest snapshot
Asked Answered
E

12

19

We've just upgraded out nexus installation to the latest release (3.x). Is there any way to get the latest version of a given snapshot artifact? Nexus 2 had a nice API which is not supported anymore.

Same question (but for the old version) has been answered here: Sonatype Nexus REST Api fetch latest build version

Any ideas are highly appreciated.

Best, Daniel

Enterovirus answered 17/5, 2016 at 15:43 Comment(2)
books.sonatype.com/nexus-book/3.0/reference/… ?Twannatwattle
looks it's about nexus plugins, not for getting data via REST APIIndoeuropean
R
18

Asof April 2019 there IS a REST API in Sonatype Nexus 3 for accessing the latest artefact

Documentation is here

http://community.sonatype.com/t/nxrm-3-16-rest-search-and-filtering-enhancements/1586

Use this endpoint /service/rest/v1/search/assets/download with repository, group and name arguments. Sorting by version will get you the latest timestamped snapshot.

https://nexus.blahblah.com/service/rest/v1/search/assets/download?repository=maven-snapshots&group=com.my.company&name=myArtefact&sort=version&direction=desc

Rogue answered 15/8, 2019 at 15:54 Comment(5)
2 shots of whiskey to this gentleman! This solved exactly my use case: having a link that automatically retrieves the most recent snapshot/release.Postlude
You can also narrow the search using wildcards, for example: &version=2.1* (tested on OSS 3.21.2)Burra
Thanks for the precise answer and example link!Longfellow
There are a few more useful parameters, e.g. &maven.baseVersion=master-SNAPSHOT&maven.classifier=sql&maven.extension=zipPartook
Also note that if using curl, you will need the -L option to actually download the found artifact, as well as the -o option to specify the output file name.Partook
I
6

Nexus 2 had a nice API which is not supported anymore.

It sounds like you are referring to these:

/service/local/artifact/maven/content
/service/local/artifact/maven/redirect

If you're asking to find the latest x.y.z-SNAPSHOT version where the x, y, z are guessed - Nexus never had this functionality (it worked only for plugins).

This is simply untrue - see the following article which shows clearly you can specify LATEST, RELEASE or SNAPSHOT base versions.

https://support.sonatype.com/hc/en-us/articles/213465488-How-can-I-retrieve-a-snapshot-if-I-don-t-know-the-exact-filename-

It's possible but not in a 1-liner.

Yes - unless you have a tool handy such as artifact-resolver which does uses a one line command to fetch an artifact.

Interim answered 9/1, 2017 at 20:38 Comment(0)
B
4

What a joke re: Nexus 3 having no REST API.

I found a hack that alleviates my problem. Turns out that ansible has a nice maven_artifact module that is somehow able to figure out the latest snapshot. And you can run ansible locally. So it ends up looking like this:

ansible all -i localhost, -c local -m maven_artifact -a "repository_url=https://my-nexus/repository/maven-snapshots/ group_id=com.whatever artifact_id=my-artifact version=2.0-SNAPSHOT dest=./my-artifact.jar"
Bipolar answered 19/1, 2017 at 22:12 Comment(1)
That doesn't explain how to download the latest snapshot if it isn't 2.0-SNAPSHOT by accident.Enchain
P
4

Bash one-liner using curl, jq, sort and tail :

NEXUS_URL=https://your-nexus.com
MAVEN_REPO=maven-snapshots
GROUP_ID=...
ARTIFACT_ID=...
VERSION=2.0.1-SNAPSHOT
FILE_EXTENSION=jar

download_url=$(curl -X GET "${NEXUS_URL}/service/rest/v1/search/assets?repository=${MAVEN_REPO}&maven.groupId=${GROUP_ID}&maven.artifactId=${ARTIFACT_ID}&maven.baseVersion=${VERSION}&maven.extension=${FILE_EXTENSION}" -H  "accept: application/json"  | jq -rc '.items | .[].downloadUrl' | sort | tail -n 1)

wget $download_url
Piquet answered 15/3, 2019 at 10:39 Comment(2)
you are great! :)Sensate
Like to echo that first comment, you are the greatest! This saved me while trying to building a docker image for deploying snapshots into our cluster. Thank you!Pinsk
P
1

If you ask for x.y.z-SNAPSHOT then by default the latest x.y.z-timestamp snapshot version will be downloaded. No need to do anything additional

If you're asking to find the latest x.y.z-SNAPSHOT version where the x, y, z are guessed - Nexus never had this functionality (it worked only for plugins). And I don't think there is any good use case for this. If this is needed, you're probably doing something wrong. You always should work with a specific version. Actually even for the 1st functionality I can't think of good use cases.

Policewoman answered 30/12, 2016 at 9:56 Comment(0)
N
1

recently we faced the same problem with nexus version 3.12.1-01, so there is definitely not rest api to get latest snapshot directly

we were able to solve the problem using python one liner

JSON_RESPONSE=$(curl -u un:pw -X GET "http://nexus-host/nexus/service/rest/beta/search/assets?maven.groupId=sample.group.id&maven.artifactId=sample&maven.extension=jar" -H  "accept: application/json")

echo $JSON_RESPONSE | python -c 'import sys, json; lines = json.load(sys.stdin)["items"]; sortedlines = sorted(lines, key=lambda k: k["downloadUrl"], reverse=True); print(sortedlines[0]["downloadUrl"])'

hope it helps

Nada answered 18/7, 2018 at 11:31 Comment(0)
W
1

You can download with curl

curl -L --header 'Accept: application/json' "https://${NEXUS_URL}/service/rest/beta/search/assets/download?repository=${NEXUS_REPO_NAME}&maven.groupId=${MVN_GROUP_ID}&maven.artifactId=${MVN_ARTIFACT_ID}&maven.baseVersion=${APP_VERSION}&maven.extension=${MVN_EXTENSION}"
Weakling answered 25/2, 2019 at 15:3 Comment(0)
M
1

Using OSS 3.21.2-03 I have retrieved latest snapshot with following url on a zip extension file :

{nexus_host}/service/rest/v1/search/assets/download?sort=version&repository={repository_name}&group={group_id}&name={artifact-id}&maven.extension=zip
Millicent answered 14/4, 2020 at 11:7 Comment(0)
O
0

It's possible but not in a 1-liner. You need to fetch the maven-metadata.xml for each snapshot artifact you're after (note that multi-module projects have different timestamps for each module including the parent).

We use xlstproc to extract the relevant variables so we are still able to run from the command line without heavyweight tools like maven or ivy to do the resolution.

Ottoottoman answered 30/12, 2016 at 9:12 Comment(0)
O
0

I've put together a groovy script that can be uploaded to Nexus which solves this particular issue with a POST request.

You can find the script and some usage instructions here: https://github.com/rbjorklin/resolve-latest-nexus-artifact

Outrank answered 1/11, 2018 at 9:14 Comment(0)
C
0

In my case, I wasn't getting anything back because I was missing the credentials require for my Nexus Server. Once I added them to both the CURL and WGET command, it worked for me.

NEXUS_URL=https://your-nexus.com
MAVEN_REPO=maven-snapshots
GROUP_ID=...
ARTIFACT_ID=...
VERSION=2.0.1-SNAPSHOT
FILE_EXTENSION=jar

download_url=$(curl --user admin:yourpassword -X GET "${NEXUS_URL}/service/rest/v1/search/assets?repository=${MAVEN_REPO}&maven.groupId=${GROUP_ID}&maven.artifactId=${ARTIFACT_ID}&maven.baseVersion=${VERSION}&maven.extension=${FILE_EXTENSION}" -H  "accept: application/json"  | jq -rc '.items | .[].downloadUrl' | sort | tail -n 1)

wget --user=admin --password=mypassword $download_url

Carcanet answered 11/1, 2021 at 6:5 Comment(0)
L
0

To sum up on top of user1717259's answer (using Nexus 3.37.3-02).

Bash one-liner using curl and sed:

BASE_URL="https://your-nexus-server.com"

# All of the following query parameters are optional, and 
# defaults to <empty> if not specified otherwise, and
# wildcards can be used as well.

params="repository=<the-repository-id>"

params+="&maven.groupId=<the-group-id>"
params+="&maven.artifactId=<the-atifact-id>"
params+="&maven.baseVersion=<the-version>"   # can be <empty>, `1.2.3`, `1.2.3-SNAPSHOT`, or `1.2.*`, etc.
params+="&maven.extendsion=<the-extension>"
params+="&maven.classifier=<the-classifier>"

params+="&sort=<the-sort-field>"             # can be <empty>, `group`, `name`, `version`, 'repository'
params+="&direction=<the-sort-direction>"    # can be `asc`, and `desc` (the default)
params+="&prerelease=<empty, true, or false>" # default (<empty>) to search releases and prereleases

DOWNLOAD_URL=$(curl -s -k -i "$BASE_URL/service/rest/v1/search/assets/download?$params" | sed -n -E 's/^Location: ([^\r\n]+).*$/\1/p')

Version comparison examples:

1.2.3 < 1.3.0-SNAPSHOT < 1.3.0-CD2-SNAPSHOT < 1.3.0-CD12-SNAPSHOT < 1.3.0-RC1 < 1.3.0 < 1.4.0-SNAPSHOT

Lossa answered 6/4, 2022 at 14:58 Comment(2)
Nexus 3.37 has a but about this right ? Is this a workaround ?Convergence
issue : issues.sonatype.org/browse/NEXUS-30694Convergence

© 2022 - 2024 — McMap. All rights reserved.