The following scripts are in Ruby and they use the REXML XML processor and the Net:HTTP library. Both of these libraries are available in the Ruby standard library. I tested these Ruby scripts using the latest JRuby release. If you want to try these scripts out on your own, they will run in any standard Ruby interpreter without requiring any extra RubyGems.
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. We're going to focus on just two services in this post: list repositories and searching for artifacts, but you should know that there are hundreds of things you can do to Nexus via the various REST interfaces that are available. In fact, you could create a custom UI tailored to your own needs that interfaces with Nexus via this REST backend. In addition to the REST services in the core Nexus installation, you can extend Nexus and add in your own REST services via a Nexus plugin. The Nexus UI is completely decoupled from the set of services Nexus provides, and the services which Nexus provides can be extended.
If you would like to download the sample scripts in this blog post, you can download this ZIP file which contains four Ruby scripts: http://books.sonatype.com/tutorial-files/nexus-rest-ruby.zip
The first script simply lists all of the repositories in a Nexus installation. Here it is:
require 'net/http' require 'rexml/document' include REXML url = 'http://repository.sonatype.org/service/local/repositories' resp = Net::HTTP.get_response( URI.parse( url ) ) doc = REXML::Document.new( resp.body ) XPath.each( doc, "//repositories-item" ) do |r| puts "#{r.elements["name"].text}(#{r.elements["id"].text})" puts "\t" + r.elements["resourceURI"].text + "\n\n" end
This script sets the pattern for the scripts to follow. We construct a URL in the script (a more general script would read this from a config file), then we make the request using Net::HTTP and we parse the results with REXML by passing it to the Document constructor. At this point, I use XPath to select all of the repositories and I print out the name, id, and the URL of the repository as served by Nexus.
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.
require 'net/http' require 'rexml/document' include REXML url = 'http://repository.sonatype.org/service/local/data_index?q=' resp = Net::HTTP.get_response( URI.parse( url + ARGV[0]) ) doc = REXML::Document.new( resp.body ) XPath.each( doc, "//data/artifact" ) do |r| puts "#{r.elements["groupId"].text}:#{r.elements["artifactId"].text}:#{r.elem\ ents["version"].text}" end
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.
require 'net/http' require 'rexml/document' include REXML url = 'http://repository.sonatype.org/service/local/data_index?cn=' resp = Net::HTTP.get_response( URI.parse( url + ARGV[0]) ) doc = REXML::Document.new( resp.body ) XPath.each( doc, "//data/artifact" ) do |r| puts "#{r.elements["groupId"].text}:#{r.elements["artifactId"].text}:#{r.elem\ ents["version"].text}" end
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.
require 'net/http' require 'rexml/document' include REXML url = "http://repository.sonatype.org/service/local/data_index?g=#{ARGV[0]}&a=#\ {ARGV[1]}&v=#{ARGV[2]}" resp = Net::HTTP.get_response( URI.parse( url ) ) doc = REXML::Document.new( resp.body ) XPath.each( doc, "//data/artifact" ) do |r| puts "#{r.elements["groupId"].text}:#{r.elements["artifactId"].text}:#{r.elem\ ents["version"].text}" end
This final script takes three command line arguments: groupId, artifactId, version, and it performs a GAV (groupId, artifactId, version) coordinate search over the repository.