Searching With the Sonatype Nexus REST API: Groovy

November 21, 2008 By Tim OBrien

4 minute read time

This post is a follow-up to the previous post which provided some sample Ruby scripts that can be used to list repositories and search for artifacts in a Nexus instance. Today, we're going to see how to complete the same tasks using the Groovy scripting language. The Groovy scripting language is a scripting language that has gained popularity due to the ease of integration with the JVM.

The following scripts are in Groovy and they can be invoked from the command line using groovy. For example, if you want to run the QuickSearch.groovy script, you would run "groovy QuickSearch.groovy activemq" to search for artfiacts that contain the string "activemq". I tested these Ruby scripts using the latest Groovy 1.5.7 distribution. Go to the Groovy page, click on Download, and download the Cross-platform Installer, this will install Groovy on your machine and also tell you what you'll need to add to your PATH.

The Nexus REST API

The UI of Nexus is written in ExtJS and it makes heavy use of AJAX callbacks to REST services. In fact, if you load up Nexus in a tool like Firebug, an extension for Firefox which allows you to trace all network activity, you will see that almost every action in Nexus triggers a call to a REST service. For more details about the Nexus REST services and for a pointer to the documentation, read the previous blog post in this series which was focused on a similar set of Ruby scripts.

If you would like to download the sample scripts in this blog post, you can download this ZIP file which contains four Groovy scripts: http://books.sonatype.com/tutorial-files/nexus-rest-groovy.zip

Listing Repositories

The first script simply lists all of the repositories in a Nexus installation. Here it is:

def xml = "http://repository.sonatype.org/service/local/repositories".toURL().text

def root = new XmlParser().parseText( xml )
root.data.'repositories-item'.each {
  println "${it.name.text()} (${it.id.text()})"
  println "\t${it.resourceURI.text()}\n"
}

This script sets the pattern for the scripts to follow. We construct a URL in String, then we call toURL() and get the text attribute from the method call. In one line we fetch the response body. then we use the XmlParser in Groovy to print out the Repository name, id, and URL. This script could not be more straightforward. This post is simply a pointer to the service and a quick demonstration so I'm not going to dive into the meaning of every single element in the XML document returned by the repositories service. If you are interested in see the full set of elements that are available, load up the results of this service in a web browser by click on this: http://repository.sonatype.org/service/local/repositories.

Performing a "Quick Search"

def xml = ("http://repository.sonatype.org/service/local/data_index?q=" + args[0]).toURL().text

def root = new XmlParser().parseText( xml )
root.data.artifact.each {
  println "${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}"
}

This script follows the pattern of the previous script to list all repositories with the exception that it reads an argument from the command line. This script performs a quick search by hitting the data_index service and passing in the q parameter. This script simply prints out the groupId:artifactId:version of all the artifacts located.

If you would like to see an example of the XML that this service produces click here: http://repository.sonatype.org/service/local/data_index?q=activemq. In the full results, you'll see more information such as the number of search hits available, the resource URL for each artifact found, and the context (or repository) in which the artifact is available.

Searching by Class Name

def xml = ("http://repository.sonatype.org/service/local/data_index?cn=" + args[0]).toURL().text

def root = new XmlParser().parseText( xml )
root.data.artifact.each {
  println "${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}"
}

This script is almost exactly the same as the prior script that performed a quick search. The difference in this script is that instead of passing the q parameter, this script passes the cn parameter. Passing the cn parameter causes Nexus to search for artifacts which contain classes that match the given value. The results are going to look the same as the quick search query script. To see the XML yourself, search for all artifacts which contain a class named HibernateDaoSupport: http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport.

Performing a GAV Search

def xml = "http://repository.sonatype.org/service/local/data_index?g=${args[0]}&a=${args[1]}&v=${args[2]}".toURL().text

def root = new XmlParser().parseText( xml )
root.data.artifact.each {
  println "${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}"
}

This final script takes three command line arguments: groupId, artifactId, version, and it performs a GAV (groupId, artifactId, version) coordinate search over the repository.

Tags: scripting, Nexus Repo Reel, rest, Everything Open Source, groovy

Written by Tim OBrien

Tim is a Software Architect with experience in all aspects of software development from project inception to developing scaleable production architectures for large-scale systems during critical, high-risk events such as Black Friday. He has helped many organizations ranging from small startups to Fortune 100 companies take a more strategic approach to adopting and evaluating technology and managing the risks associated with change.