Getting the Latest Snapshot From Sonatype Nexus
Posted on January 15, 2013 • 2 min read • 316 wordsSonatype Nexus is a repository for build artifacts, which is particularly handy if you have a Maven project. Once you have your Maven project configured, every time you run mvn deploy Maven will do a bit of building and then upload the resulting artifacts (.jar, .war, …) to the repository. If you browse Nexus you will then be able to find those artifacts with a unique name and download them.
This is all great, but if your project is running on a snapshot version, then every time you deploy to Nexus, the artifact file name will be appended with date and an ever-incrementing number. For my purposes, I wanted to be able to go on to a Linux test server where I have Apache Tomcat installed and grab the latest .war file. Maybe I need to relax more, but I was getting a bit irritated with having to manually find the snapshot in Nexus, copy the link and then fire off a curl -O -L http://… command every time I updated the project.
Fortunately it turns out that Nexus provides a REST API for searching. Unfortunately, it only returns the name of an artifact without the time-stamp. I think that this is intended to be a ‘good thing’ with Nexus automatically resolving the latest snapshot based on requesting the snapshot with no time-stamp. Unfortunately, requesting that artifact from the location indicated by the API results in a ‘not found’ response.
Therefore I knocked up a little Ruby script, which will go to the URI at which a full artifact list can be found, which includes resources such as poms, jars, sha1 and md5 hashes. It then parses the response XML to narrow down the results and selects the most recent artifact that matches the search criteria. Finally it will download the artifact.
The latest version of the script can be found as a gist on GitHub: