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.

Giorgio
  • 13,129
  • 12
  • 48
  • 75
  • 1
    possible duplicate of [Using the Nexus rest API to get latest artifact version for given groupid/artficatid](http://stackoverflow.com/questions/7911620/using-the-nexus-rest-api-to-get-latest-artifact-version-for-given-groupid-artfic) – Mark O'Connor Feb 09 '13 at 09:37

5 Answers5

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
Wenbing Li
  • 12,289
  • 1
  • 29
  • 41
Ido.Co
  • 5,317
  • 6
  • 39
  • 64
  • you may like to use &e=war or &e=jar like steinim suggested below (btw his solution was not working for me) – moshe beeri Mar 25 '14 at 13:50
  • 2
    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: http://articles.javatalks.ru/articles/32 – Giuseppe Feb 06 '17 at 15:46
  • 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=zip` – schnatterer Feb 20 '17 at 16:15
  • Indeed it doesnt work for nexus 3. Do you have any other solution? – Mohamed Thoufeeque Oct 16 '17 at 14:47
  • 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... – FranAguiar Nov 15 '17 at 15:09
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}"
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
Reza Rahimi
  • 958
  • 11
  • 16
4

Lucene search API also allow keyword search for version:

http://<nexus_repository>/nexus/service/local/lucene/search?a=ARTIFACT_NAME&v=1.0.*
Henrique Gontijo
  • 1,052
  • 2
  • 15
  • 27
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
Community
  • 1
  • 1
idelvall
  • 1,536
  • 15
  • 25