Sonatype Nexus REST Api fetch latest build version

42,752

Solution 1

It is not documented that /service/local/lucene/search support "LATEST" as version parameter [link] The OSS rest api documentation states that /service/local/artifact/maven [link] (to get the artifact pom file) and /service/local/artifact/maven/content [link] (to get the actual file content) does support it:

Version of the artifact (Required) Supports resolving of "LATEST", "RELEASE" and snapshot versions ("1.0-SNAPSHOT") too.

So I think you should use one of them (you will have to supply them also with repositoryId and groupId) for example:

http://MY_REPOSITORY/nexus/service/local/artifact/maven/content?r=repoId&g=groupName&a=art&v=LATEST

Solution 2

I had the same problem and solved it like this using the lucene search api:

if [[ "${REPO}" == "snapshots" ]]; then
  version=$( curl --silent "http://${HOST}/nexus/service/local/lucene/search?g=${GROUP_ID}&a=${ARTIFACT}" | sed -n 's|<latestSnapshot>\(.*\)</latestSnapshot>|\1|p' | sed -e 's/^[ \t]*//' | tail -1 )
else
  version=$( curl --silent "http://${HOST}/nexus/service/local/lucene/search?g=${GROUP_ID}&a=${ARTIFACT}" | sed -n 's|<latestRelease>\(.*\)</latestRelease>|\1|p' | sed -e 's/^[ \t]*//' | tail -1 )
fi

curl -o ~/${ARTIFACT}-${VERSION}.zip -L -#  "http://${HOST}/nexus/service/local/artifact/maven/redirect?r=${REPO}&g=${GROUP_ID}&a=${ARTIFACT}&e=zip&v=${VERSION}"

Solution 3

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

Solution 4

Lucene search API also allow keyword search for version:

http://<nexus_repository>/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=1.0.*

Solution 5

After trying the REST service with the LATEST version (and discovering it doesn't always work) 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
Share:
42,752

Related videos on Youtube

Giorgio
Author by

Giorgio

Updated on May 22, 2020

Comments

  • Giorgio
    Giorgio almost 4 years

    How can I can use the Sonatype REST Api to fetch the build with the highest version (latest temporal build)?

    http://MY_REPOSITORY/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=ARTIFACT_VERSION
    

    Passing a build version as ARTIFACT_VERSION works. Passing v=LATEST or v=latest does NOT return the latest build.

  • moshe beeri
    moshe beeri about 10 years
    you may like to use &e=war or &e=jar like steinim suggested below (btw his solution was not working for me)
  • Giuseppe
    Giuseppe over 7 years
    Be careful when using this: if the latest version has been set to a fixed value in the metadata (on Sonatype Nexus it happens when "Rebuild metadata" is hit), you could get an old version of your artifact instead of the latest, as explained here: articles.javatalks.ru/articles/32
  • schnatterer
    schnatterer about 7 years
    If you're artifact is not a jar, add the e parameter, for example: http://MY_REPOSITORY/nexus/service/local/artifact/maven/cont‌​ent?r=repoId&g=group‌​Name&a=art&v=LATEST&‌​e=zip
  • Mohamed Thoufeeque
    Mohamed Thoufeeque over 6 years
    Indeed it doesnt work for nexus 3. Do you have any other solution?
  • FranAguiar
    FranAguiar over 6 years
    That solution isn't working for me. I trying this and I have and artifact that should match: curl -u admin:admin123 -L "http://127.0.0.1:8081/nexus/service/local/artifact/maven/co‌​ntent?r=maven-group&‌​g=xml-apis&a=art&v=L‌​ATEST" But I get a 404 error...
  • him
    him about 4 years
    instead of "grep" i would recommend xmllint with the --xpath flag
  • him
    him about 4 years
    instead of "grep" i would recommend xmllint with the --xpath flag