Sonatype Nexus REST Api fetch latest build version
Asked Answered
B

5

20

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.

Boneyard answered 9/2, 2013 at 1:12 Comment(1)
possible duplicate of Using the Nexus rest API to get latest artifact version for given groupid/artficatidRosenbaum
S
30

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
Schnorrer answered 10/2, 2013 at 15:50 Comment(5)
you may like to use &e=war or &e=jar like steinim suggested below (btw his solution was not working for me)Naze
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/32Nutcracker
If you're artifact is not a jar, add the e parameter, for example: http://MY_REPOSITORY/nexus/service/local/artifact/maven/content?r=repoId&g=groupName&a=art&v=LATEST&e=zipChurch
Indeed it doesnt work for nexus 3. Do you have any other solution?Ebner
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/content?r=maven-group&g=xml-apis&a=art&v=LATEST" But I get a 404 error...Borchers
E
18

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}"
Encyclopedic answered 8/5, 2013 at 11:52 Comment(0)
I
5

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
Inconstant answered 14/9, 2016 at 8:2 Comment(1)
instead of "grep" i would recommend xmllint with the --xpath flagBono
P
4

Lucene search API also allow keyword search for version:

http://<nexus_repository>/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=1.0.*
Pancratium answered 24/9, 2013 at 23:39 Comment(0)
B
2

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
Bikaner answered 2/11, 2016 at 10:50 Comment(1)
instead of "grep" i would recommend xmllint with the --xpath flagBono

© 2022 - 2024 — McMap. All rights reserved.