Sonatype Nexus 3 - get latest snapshot

╄→гoц情女王★ 提交于 2019-12-18 14:52:37

问题


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


回答1:


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.




回答2:


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"



回答3:


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.




回答4:


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




回答5:


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




回答6:


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.




回答7:


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




回答8:


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



回答9:


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


来源:https://stackoverflow.com/questions/37280818/sonatype-nexus-3-get-latest-snapshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!