Using the Nexus rest API to get latest artifact version for given groupid/artifactId
Asked Answered
C

7

54

I am trying to use the nexus REST api to get the latest version of a maven artifact. I am able to browse to the specific version I am looking for using http://repo.local/service/local/data_index?a=local-turbogears-server&from=0&g=com.turbo&c=bin&v=1.1.9 and if I remove the version parameter I can see every version. However when I try and use RELEASE or LATEST as the version then it returns zero results. I checked the maven-metadata.xml on disk in nexus and there are entries for latest and release. Is there another step I need to take to return the latest version?

I am currently using:

Nexus v. 1.9.2

Chaney answered 27/10, 2011 at 4:8 Comment(0)
D
76

The following URL will retrieve the latest version of log4j 1.2.x:

http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=log4j&a=log4j&v=LATEST

Documented here

Update

Example using curl:

curl -L "http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=log4j&a=log4j&v=LATEST" -o log4j.jar

Update for Log4j2

Log4j 1.2 is EOL since summer 2015 and is known to be broken in Java 9.

Here is the link for the Log4j artifacts:

Djakarta answered 27/10, 2011 at 21:49 Comment(7)
I was able to download the latest version of the artifact by replacing local/data_index? with local/artifact/maven/redirect? and adding a repository parameter. Also thanks for the link to the documentation =)Chaney
what if the artifact is not a jar file? I have published an Android aar libraryPredicament
@DeanWild add &p=aarTieback
@DeanWild The search results screen (available from the Nexus UI) will contain links to the artifacts.Salop
@Mark O'Connor can we do the same from unix command line using curlRuin
@Ruin Sure you can. The trick is to use the "-L" option because Nexus returns a 301 redirect. I have updated my answer with an exampleSalop
I can't get this to work in a company internal Nexus. How can I figure out the difference between the link you provide and the link I need in my system? I only get HTTP 404 and that's it.Rexferd
B
27

In Nexus LATEST is designed to work with maven plugins rather than with regular artifacts. Nexus simply doesn't guarantee the LATEST to work in other cases. If right now it returns you the correct version of the artifact, tomorrow this may stop working e.g. if you run Rebuild Metadata for the Nexus repository. Do you want to rely on the mechanism that may break at any moment (e.g. during the release process?). I doubt. Read this article for more insight.

In order to find the LATEST artifact version you should either write your own script to invoke Search API and sort artifact versions as you want. Or you can write your own plugin for Nexus.

Alternatively, if your workflow allows that, you can use SNAPSHOTs instead of release versions. If you don't increment numeric part, then x.y.z-SNAPSHOT will always return the latest binary.

Last point: do not use latest versions of artifacts, in the vast majority of cases if you have such a use case, then something is wrong with your deployments (or whatever you're doing). In general you should know the exact version you're going to use.

Bergstein answered 20/9, 2014 at 17:43 Comment(3)
A simple example for "latest version" would be a developer environment. devs usually want to do things on the most up to date build, e.g. checking how their latest change affects the complete software they are working on.Rexferd
For that you build a pipeline - the version is determined on the Build stage and then is passed to the next jobs. You never use LATEST for this at least because you'll end up with different version deployed in different parts of the pipeline (due to the race conditions).Bergstein
You don't add this to your automation steps of course. But your developer uses that to manually wget the latest install_bundle.tar.gz for instance (or pip install <url> etc)Rexferd
A
21

This answer has been copied from: https://mcmap.net/q/339736/-sonatype-nexus-rest-api-fetch-latest-build-version

I have Linux OS and I do not have access to REST API, so I used following commands to get the latest version of snapshots from Nexus:

An example snapshots maven-metadata.xml from WSO2 repository:

$ curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml"
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>org.wso2.is</groupId>
  <artifactId>wso2is</artifactId>
  <versioning>
    <latest>5.3.0-SNAPSHOT</latest>
    <release></release>
    <versions>
      <version>5.1.0-SNAPSHOT</version>
      <version>5.2.0-SNAPSHOT</version>
      <version>5.3.0-SNAPSHOT</version>
    </versions>
    <lastUpdated>20160914062755</lastUpdated>
  </versioning>
</metadata>

Extracting from latest XML tag inside maven-metadata.xml:

curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml" | \
grep "<latest>.*</latest>" | \
sed -e "s#\(.*\)\(<latest>\)\(.*\)\(</latest>\)\(.*\)#\3#g"

Extracting from version XML tag inside maven-metadata.xml:

curl -s "http://maven.wso2.org/nexus/content/repositories/snapshots/org/wso2/is/wso2is/maven-metadata.xml" | \
grep "<version>.*</version>" | \
sort --version-sort | uniq | tail -n1 | \
sed -e "s#\(.*\)\(<version>\)\(.*\)\(</version>\)\(.*\)#\3#g"

The result of both of the commands until today 14 Sep 2016 is:

5.3.0-SNAPSHOT
Assyrian answered 14/9, 2016 at 8:5 Comment(4)
Not sure why all the down votes? This approach is particularly useful for those of us using Nexus 3, which no long has the REST APIDrury
I know that there is a badge in Stackoverflow which reaches by giving a negative vote a post. For this reason many person search and vote negative the first thing that they could not undrestand to reach the badge :)Assyrian
Works great with Nexus 3. I'd just like to add: sort --version-sort works better: it will consider 1.0.9, 1.0.10, 1.0.11 as the correct order instead of alphabetic ordering, which is default (1.0.10, 1.0.11, 1.0.9)Rarotonga
Reading maven-metadata.xml is a good idea, but it seems the <latest> element is not reliable as described in other answers, see articles.javatalks.ru/articles/32.Wrapper
H
5

After trying the REST service with the LATEST version, and discovering it doesn't always work (See @Stanislav response) I ended up creating this one-liner Linux command for parsing the metadata.xml file:

wget -O - -o /dev/null https://repo1.maven.org/maven2/org/brutusin/wava/maven-metadata.xml | grep -Po '(?<=<version>)([0-9\.]+(-SNAPSHOT)?)' | sort --version-sort -r | head -n 1

Just change the to the appropiate url, and it should work for you.

Cheers

Hoffer answered 2/11, 2016 at 10:56 Comment(2)
For Nexus 3.0 and above <latest> no longer exists, you can use <release>Fellner
@Fellner I updated the script to support both optionsHoffer
W
5

For latest versions of Nexus (since 3.16.0): Example of downloading the latest version (stored in format of zip file) from maven releases repository, using browser:

http://<YourNexusUrl>/service/rest/v1/search/assets/download?sort=version&repository=maven-releases&maven.groupId=<yourGroupID>&maven.artifactId=<yourArtifactID>&maven.extension=zip

Using curl:

curl -L -X GET "http://<YourNexusUrl>/service/rest/v1/search/assets/download?sort=version&repository=maven-releases&maven.groupId=<yourGroupID>&maven.artifactId=<yourArtifactID>&maven.extension=zip" --output myZip.zip

Wirewove answered 14/9, 2020 at 17:30 Comment(0)
L
4

In the last version of Nexus API is documented the way to retrieve the latest version of a component, filtering by repository, group, artifact and base version

Example: http://localhost:8081/service/rest/v1/search/assets/download?sort=version&repository=maven-snapshots&maven.groupId=org.foo.bar&maven.artifactId=project&maven.baseVersion=1.2.3-SNAPSHOT&maven.extension=jar

Reference documentation: https://help.sonatype.com/repomanager3/rest-and-integration-api/search-api#SearchAPI-SearchComponents

Lafayette answered 25/6, 2020 at 11:3 Comment(0)
S
1

Another alternative of downloading the latest artifact from Nexus using "jq"

artifact_url="https://<nexus url>/service/rest/v1/search/assets?group=<group name>&name=<artifact_name>&maven.baseVersion=<artifact_base_version>&maven.extension=zip"

# Get data of the latest artifact
artifact_data=$(curl -s "${artifact_url}" | jq '.items |= sort_by(.lastModified)' | jq '.items[-1]' --raw-output)

# Get the download Url of the artifact
artifact_download_url=$(echo "${artifact_data}" | jq '.downloadUrl' --raw-output)

# Downloads
curl --create-dirs -LsSf -o "./artifacts/<artifact name>.zip" "${artifact_download_url}"
Sycophant answered 5/7, 2023 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.