<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sonatype Blog &#187; scripting</title>
	<atom:link href="http://blog.sonatype.com/people/tag/scripting/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Thu, 16 May 2013 18:53:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Searching with the Sonatype Nexus REST API: Python</title>
		<link>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-python/</link>
		<comments>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-python/#comments</comments>
		<pubDate>Mon, 24 Nov 2008 13:00:46 +0000</pubDate>
		<dc:creator>Tim O'Brien</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Nexus]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blogs.sonatype.com/people/book/?p=292</guid>
		<description><![CDATA[This post is a follow-up to the two previous posts on Ruby and Groovy which provided some sample Ruby and Groovy scripts that can be used to list repositories and search for artifacts in a Nexus instance. Today, we&#8217;re going to see how to complete the same tasks using the Python scripting language. The following [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-294" src="http://cms.sonatype.com/wordpress/wp-content/uploads/2008/11/scripting-nexus-with-python.png" alt="" width="115" height="99" />This post is a follow-up to the two previous posts on <a href="http://blogs.sonatype.com/people/book/2008/11/19/searching-with-the-nexus-rest-api-ruby/">Ruby</a> and <a href="http://blogs.sonatype.com/people/book/2008/11/21/searching-with-the-sonatype-nexus-rest-api-groovy/">Groovy</a> which provided some sample <a href="http://blogs.sonatype.com/people/book/2008/11/19/searching-with-the-nexus-rest-api-ruby/">Ruby</a> and <a href="http://blogs.sonatype.com/people/book/2008/11/21/searching-with-the-sonatype-nexus-rest-api-groovy/">Groovy</a> scripts that can be used to list repositories and search for artifacts in a Nexus instance.   Today, we&#8217;re going to see how to complete the same tasks using the Python scripting language.</p>

<p>The following scripts are in Python and they can be invoked from the command line using python (or jython).  For example, if you want to run the quick-search.py script, you would run &#8220;jython quick-search.py activemq&#8221; to search for artfiacts that contain the string &#8220;activemq&#8221;.   I tested these Python scripts using the <a href="http://www.jython.org/Project/">latest Jython 2.5b0</a> distribution with <a href="http://code.google.com/p/jython-elementtree/">version 1.2.6 of the jython-elementtree project</a> at Google Code.  Go to the Jython page, <a href="http://www.jython.org/Project/">download Jython 2.5b0</a>, and then following <a href="http://code.google.com/p/jython-elementtree/">the instructions on the jython-elementtree</a> project list to check out the ElementTree library from source control and install it in your own Jython installation.  These scripts don&#8217;t require Jython, the could run in any Python 2.5 implementation.<span id="more-346"></span></p>

<h2>The Nexus REST API</h2>

<p>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 <a href="http://blogs.sonatype.com/people/book/2008/11/19/searching-with-the-nexus-rest-api-ruby/">previous blog post</a> in this series which was focused on a similar set of Ruby scripts.</p>

<p>If you would like to download the sample scripts in this blog post, you can download this ZIP file which contains four Python scripts: <a href="http://books.sonatype.com/tutorial-files/nexus-rest-python.zip">http://books.sonatype.com/tutorial-files/nexus-rest-python.zip</a></p>

<h2>Listing Repositories</h2>

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


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>,<span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementTree
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementPath
&nbsp;
url = <span style="color: #483d8b;">'http://repository.sonatype.org/service/local/repositories'</span>
data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> url <span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
doc = ElementTree.<span style="color: black;">XML</span><span style="color: black;">&#40;</span> data <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> ElementPath.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>doc, <span style="color: #483d8b;">&quot;.//repositories-item&quot;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span> a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;name&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot; (&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;id&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;resourceURI&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> <span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></pre></div></div>


<p>This script sets the pattern for the scripts to follow.  We construct a URL, then we call urlopen and read the response body into a String.  Then we use ElementTree to parse the XML document.  Once the document is parsed, we use ElementPath to apply our XPath expression to the document.   Finally the repository, the repository id, and the repository URL is printed out.  This script could not be more straightforward.  This post is simply a pointer to the service and a quick demonstration so I&#8217;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: <a href="http://repository.sonatype.org/service/local/repositories">http://repository.sonatype.org/service/local/repositories</a>.</p>

<h2>Performing a &#8220;Quick Search&#8221;</h2>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>,<span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementTree
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementPath
&nbsp;
url = <span style="color: #483d8b;">'http://repository.sonatype.org/service/local/data_index?q='</span>
data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> url + <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
doc = ElementTree.<span style="color: black;">XML</span><span style="color: black;">&#40;</span> data <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> ElementPath.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>doc, <span style="color: #483d8b;">&quot;.//artifact&quot;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span> a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;groupId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;artifactId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;version&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> <span style="color: black;">&#41;</span></pre></div></div>


<p>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 <em>data_index</em> service and passing in the <em>q</em> parameter.   This script simply prints out the <em>groupId:artifactId:version</em> of all the artifacts located.</p>

<p>If you would like to see an example of the XML that this service produces click here: <a title="http://repository.sonatype.org/service/local/data_index?q=activemq" href="http://repository.sonatype.org/service/local/data_index?q=activemq">http://repository.sonatype.org/service/local/data_index?q=activemq</a>. In the full results, you&#8217;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.</p>

<h2>Searching by Class Name</h2>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>,<span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementTree
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementPath
&nbsp;
url = <span style="color: #483d8b;">'http://repository.sonatype.org/service/local/data_index?cn='</span>
data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> url + <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
doc = ElementTree.<span style="color: black;">XML</span><span style="color: black;">&#40;</span> data <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> ElementPath.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>doc, <span style="color: #483d8b;">&quot;.//artifact&quot;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span> a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;groupId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;artifactId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;version&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> <span style="color: black;">&#41;</span></pre></div></div>


<p>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: <a href="http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport">http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport</a>.</p>

<h2>Performing a GAV Search</h2>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>,<span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementTree
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementPath
&nbsp;
url = <span style="color: #483d8b;">'http://repository.sonatype.org/service/local/data_index?'</span>
url = url + <span style="color: #483d8b;">&quot;g=&quot;</span> + <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;&amp;amp;a=&quot;</span> + <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;&amp;amp;v=&quot;</span> + <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>
data = <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span> url <span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
doc = ElementTree.<span style="color: black;">XML</span><span style="color: black;">&#40;</span> data <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> a <span style="color: #ff7700;font-weight:bold;">in</span> ElementPath.<span style="color: black;">findall</span><span style="color: black;">&#40;</span>doc, <span style="color: #483d8b;">&quot;.//artifact&quot;</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#40;</span> a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;groupId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;artifactId&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> + <span style="color: #483d8b;">&quot;:&quot;</span> +
        a.<span style="color: black;">find</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;version&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">text</span> <span style="color: black;">&#41;</span></pre></div></div>


<p>This final script takes three command line arguments: groupId, artifactId, version, and it performs a GAV (groupId, artifactId, version) coordinate search over the repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching with the Sonatype Nexus REST API: Groovy</title>
		<link>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-groovy/</link>
		<comments>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-groovy/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 21:25:30 +0000</pubDate>
		<dc:creator>Tim O'Brien</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[Nexus]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blogs.sonatype.com/people/book/?p=284</guid>
		<description><![CDATA[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&#8217;re going to see how to complete the same tasks using the Groovy scripting language.   The Groovy scripting language is a scripting language that [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-285" src="http://cms.sonatype.com/wordpress/wp-content/uploads/2008/11/scripting-nexus-groovy.png" alt="" width="115" height="99" />This post is a follow-up to the <a href="http://blogs.sonatype.com/people/book/2008/11/19/searching-with-the-nexus-rest-api-ruby/">previous post</a> which provided some sample Ruby scripts that can be used to list repositories and search for artifacts in a Nexus instance.   Today, we&#8217;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.</p>

<p>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 &#8220;groovy QuickSearch.groovy activemq&#8221; to search for artfiacts that contain the string &#8220;activemq&#8221;.   I tested these Ruby scripts using the latest Groovy 1.5.7 distribution.  Go to the <a href="http://groovy.codehaus.org">Groovy page</a>, click on Download, and download the Cross-platform Installer, this will install Groovy on your machine and also tell you what you&#8217;ll need to add to your PATH.<span id="more-320"></span></p>

<h2>The Nexus REST API</h2>

<p>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 <a href="http://blogs.sonatype.com/people/book/2008/11/19/searching-with-the-nexus-rest-api-ruby/">previous blog post</a> in this series which was focused on a similar set of Ruby scripts.</p>

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

<h2>Listing Repositories</h2>

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


<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> xml <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;http://repository.sonatype.org/service/local/repositories&quot;</span>.<span style="color: #CC0099;">toURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> root <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlParser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parseText</span><span style="color: #66cc66;">&#40;</span> xml <span style="color: #66cc66;">&#41;</span>
root.<span style="color: #006600;">data</span>.<span style="color: #ff0000;">'repositories-item'</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${it.name.text()} (${it.id.text()})&quot;</span>
  <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>${it.resourceURI.text()}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>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&#8217;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: <a href="http://repository.sonatype.org/service/local/repositories">http://repository.sonatype.org/service/local/repositories</a>.</p>

<h2>Performing a &#8220;Quick Search&#8221;</h2>


<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> xml <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://repository.sonatype.org/service/local/data_index?q=&quot;</span> <span style="color: #66cc66;">+</span> args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #CC0099;">toURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> root <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlParser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parseText</span><span style="color: #66cc66;">&#40;</span> xml <span style="color: #66cc66;">&#41;</span>
root.<span style="color: #006600;">data</span>.<span style="color: #006600;">artifact</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>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 <em>data_index</em> service and passing in the <em>q</em> parameter.   This script simply prints out the <em>groupId:artifactId:version</em> of all the artifacts located.</p>

<p>If you would like to see an example of the XML that this service produces click here: <a title="http://repository.sonatype.org/service/local/data_index?q=activemq" href="http://repository.sonatype.org/service/local/data_index?q=activemq">http://repository.sonatype.org/service/local/data_index?q=activemq</a>. In the full results, you&#8217;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.</p>

<h2>Searching by Class Name</h2>


<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> xml <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://repository.sonatype.org/service/local/data_index?cn=&quot;</span> <span style="color: #66cc66;">+</span> args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #CC0099;">toURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> root <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlParser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parseText</span><span style="color: #66cc66;">&#40;</span> xml <span style="color: #66cc66;">&#41;</span>
root.<span style="color: #006600;">data</span>.<span style="color: #006600;">artifact</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>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: <a href="http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport">http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport</a>.</p>

<h2>Performing a GAV Search</h2>


<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> xml <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;http://repository.sonatype.org/service/local/data_index?g=${args[0]}&amp;amp;a=${args[1]}&amp;amp;v=${args[2]}&quot;</span>.<span style="color: #CC0099;">toURL</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">text</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> root <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlParser<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">parseText</span><span style="color: #66cc66;">&#40;</span> xml <span style="color: #66cc66;">&#41;</span>
root.<span style="color: #006600;">data</span>.<span style="color: #006600;">artifact</span>.<span style="color: #663399;">each</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;${it.groupId.text()}:${it.artifactId.text()}:${it.version.text()}&quot;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>


<p>This final script takes three command line arguments: groupId, artifactId, version, and it performs a GAV (groupId, artifactId, version) coordinate search over the repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2008/11/searching-with-the-sonatype-nexus-rest-api-groovy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Searching with the Sonatype Nexus REST API: Ruby</title>
		<link>http://blog.sonatype.com/people/2008/11/searching-with-the-nexus-rest-api-ruby/</link>
		<comments>http://blog.sonatype.com/people/2008/11/searching-with-the-nexus-rest-api-ruby/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 16:11:49 +0000</pubDate>
		<dc:creator>Tim O'Brien</dc:creator>
				<category><![CDATA[How-To]]></category>
		<category><![CDATA[Nexus]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://blogs.sonatype.com/people/book/?p=255</guid>
		<description><![CDATA[When you search for artifacts using http://repository.sonatype.org, the browser is querying the Nexus repository using a REST API.   In this post, I&#8217;m going to show you some simple Ruby scripts which you can use to search the Maven repository without loading up the Nexus web interface.  You might find these scripts more convenient and more [...]]]></description>
				<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-262" src="http://cms.sonatype.com/wordpress/wp-content/uploads/2008/11/scripting-nexus-ruby.png" alt="" width="103" height="102" />When you search for artifacts using <a href="http://repository.sonatype.org">http://repository.sonatype.org</a>, the browser is querying the Nexus repository using a REST API.   In this post, I&#8217;m going to show you some simple Ruby scripts which you can use to search the Maven repository without loading up the Nexus web interface.  You might find these scripts more convenient and more customizable, and you should feel free to copy and modify them for your own use.</p>

<p>The following scripts are in Ruby and they use the <a href="http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html">REXML</a> XML processor and the <a href="http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html">Net:HTTP</a> library.  Both of these libraries are available in the Ruby standard library.   I tested these Ruby scripts using the latest <a href="http://jruby.codehaus.org/">JRuby</a> 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.<span id="more-302"></span></p>

<h2>The Nexus REST API</h2>

<p>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&#8217;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.</p>

<p><a href="http://cms.sonatype.com/wordpress/wp-content/uploads/2008/11/nexus-rest-arch.png"><img class="aligncenter size-full wp-image-263" src="http://cms.sonatype.com/wordpress/wp-content/uploads/2008/11/nexus-rest-arch.png" alt="" width="386" height="201" /></a>The Nexus REST Services are <a href="https://docs.sonatype.com/display/Nx/Nexus+Rest+API">documented here</a>.  In this post, we&#8217;re going to be writing scripts that hit the public instance of Sonatype Nexus at <a href="http://repository.sonatype.org">http://repository.sonatype.org</a>, our REST URLs are going to look something like this: <a href="http://repository.sonatype.org/service/local/data_index?q=maven">http://repository.sonatype.org/service/local/data_index?q=maven</a>.  If you were attempting to hit the services on your local Nexus installation, your REST URL would look more like this: http://localhost:8081/nexus/service/local/data_index?cn=HibernateDaoSupport.   These REST URLs follow the pattern:  &#8220;http://{host}:{port}/{context}/service/{instance}/{service}?{query_params}&#8221; where the {instance} will almost always be &#8220;local&#8221;.</p>

<p>If you would like to download the sample scripts in this blog post, you can download this ZIP file which contains four Ruby scripts: <a href="http://books.sonatype.com/tutorial-files/nexus-rest-ruby.zip">http://books.sonatype.com/tutorial-files/nexus-rest-ruby.zip</a></p>

<h2>Listing Repositories</h2>

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


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rexml/document'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> REXML
&nbsp;
url = <span style="color:#996600;">'http://repository.sonatype.org/service/local/repositories'</span>
resp = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span> url <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> resp.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
XPath.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span> doc, <span style="color:#996600;">&quot;//repositories-item&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{r.elements[&quot;</span>name<span style="color:#996600;">&quot;].text}(#{r.elements[&quot;</span>id<span style="color:#996600;">&quot;].text})&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\t</span>&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> r.<span style="color:#9900CC;">elements</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;resourceURI&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">text</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span><span style="color:#000099;">\n</span>&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>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.</p>

<p>This post is simply a pointer to the service and a quick demonstration so I&#8217;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: <a href="http://repository.sonatype.org/service/local/repositories">http://repository.sonatype.org/service/local/repositories</a>.</p>

<h2>Performing a &#8220;Quick Search&#8221;</h2>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rexml/document'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> REXML
&nbsp;
url = <span style="color:#996600;">'http://repository.sonatype.org/service/local/data_index?q='</span>
resp = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span> url <span style="color:#006600; font-weight:bold;">+</span> ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> resp.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
XPath.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span> doc, <span style="color:#996600;">&quot;//data/artifact&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{r.elements[&quot;</span>groupId<span style="color:#996600;">&quot;].text}:#{r.elements[&quot;</span>artifactId<span style="color:#996600;">&quot;].text}:#{r.elem<span style="color:#000099;">\</span>
ents[&quot;</span>version<span style="color:#996600;">&quot;].text}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>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 <em>data_index</em> service and passing in the <em>q</em> parameter.   This script simply prints out the <em>groupId:artifactId:version</em> of all the artifacts located.</p>

<p>If you would like to see an example of the XML that this service produces click here: <a title="http://repository.sonatype.org/service/local/data_index?q=activemq" href="http://repository.sonatype.org/service/local/data_index?q=activemq">http://repository.sonatype.org/service/local/data_index?q=activemq</a>. In the full results, you&#8217;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.</p>

<h2>Searching by Class Name</h2>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rexml/document'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> REXML
&nbsp;
url = <span style="color:#996600;">'http://repository.sonatype.org/service/local/data_index?cn='</span>
resp = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span> url <span style="color:#006600; font-weight:bold;">+</span> ARGV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> resp.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
XPath.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span> doc, <span style="color:#996600;">&quot;//data/artifact&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{r.elements[&quot;</span>groupId<span style="color:#996600;">&quot;].text}:#{r.elements[&quot;</span>artifactId<span style="color:#996600;">&quot;].text}:#{r.elem<span style="color:#000099;">\</span>
ents[&quot;</span>version<span style="color:#996600;">&quot;].text}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>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: <a href="http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport">http://repository.sonatype.org/service/local/data_index?cn=HibernateDaoSupport</a>.</p>

<h2>Performing a GAV Search</h2>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rexml/document'</span>
<span style="color:#9966CC; font-weight:bold;">include</span> REXML
&nbsp;
url = <span style="color:#996600;">&quot;http://repository.sonatype.org/service/local/data_index?g=#{ARGV[0]}&amp;amp;a=#<span style="color:#000099;">\</span>
{ARGV[1]}&amp;amp;v=#{ARGV[2]}&quot;</span>
resp = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#CC00FF; font-weight:bold;">URI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span> url <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
doc = <span style="color:#6666ff; font-weight:bold;">REXML::Document</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span> resp.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
XPath.<span style="color:#9900CC;">each</span><span style="color:#006600; font-weight:bold;">&#40;</span> doc, <span style="color:#996600;">&quot;//data/artifact&quot;</span> <span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{r.elements[&quot;</span>groupId<span style="color:#996600;">&quot;].text}:#{r.elements[&quot;</span>artifactId<span style="color:#996600;">&quot;].text}:#{r.elem<span style="color:#000099;">\</span>
ents[&quot;</span>version<span style="color:#996600;">&quot;].text}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>


<p>This final script takes three command line arguments: groupId, artifactId, version, and it performs a GAV (groupId, artifactId, version) coordinate search over the repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2008/11/searching-with-the-nexus-rest-api-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
