51

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

rpax
  • 4,468
  • 7
  • 33
  • 57
chrisst
  • 1,696
  • 5
  • 19
  • 32

7 Answers7

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:

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • 1
    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 =) – chrisst Oct 27 '11 at 23:08
  • what if the artifact is not a jar file? I have published an Android aar library – Dean Wild Jul 15 '14 at 11:50
  • 2
    @DeanWild add `&p=aar` – sbk Jul 31 '14 at 14:37
  • 1
    @DeanWild The search results screen (available from the Nexus UI) will contain links to the artifacts. – Mark O'Connor Jul 31 '14 at 22:06
  • @Mark O'Connor can we do the same from unix command line using curl – Balualways Sep 10 '14 at 17:09
  • @Balualways Sure you can. The trick is to use the "-L" option because Nexus returns a 301 redirect. I have updated my answer with an example – Mark O'Connor Sep 10 '14 at 22:36
  • 3
    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. – erikbstack Oct 16 '17 at 11:20
26

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.

Stanislav Bashkyrtsev
  • 14,470
  • 7
  • 42
  • 45
  • 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. – erikbstack Oct 16 '17 at 11:13
  • 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). – Stanislav Bashkyrtsev Oct 16 '17 at 14:04
  • 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 ` etc) – erikbstack Oct 17 '17 at 14:09
21

This answer has been copied from: https://stackoverflow.com/a/39485361/1450799

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
Reza Rahimi
  • 958
  • 11
  • 16
  • 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 API – Ryan J. McDonough Nov 30 '16 at 16:50
  • 2
    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 :) – Reza Rahimi Nov 30 '16 at 17:05
  • 2
    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) – Samuel Jun 01 '18 at 17:06
  • Reading maven-metadata.xml is a good idea, but it seems the element is not reliable as described in other answers, see https://articles.javatalks.ru/articles/32. – Marcus Philip Dec 20 '19 at 13:13
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

Community
  • 1
  • 1
idelvall
  • 1,536
  • 15
  • 25
4

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

Thomas Brooks
  • 321
  • 3
  • 7
3

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

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