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

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:

  • log4j-api: https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=org.apache.logging.log4j&a=log4j-api&v=LATEST
  • log4j-core: https://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=org.apache.logging.log4j&a=log4j-core&v=LATEST

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.


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