cogUses REST and Groovy to integrate with Nexus
Developed by

Vincent Massol

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Shows how to integrate with Nexus using its REST API, using Groovy to perform the REST calls.

Examples

Use the following content in wiki pages to test them out.

List all Repositories in a Nexus instance

{{groovy}}
import groovy.xml.*

def url = "http://nexus.xwiki.org/nexus/service/local/repositories".toURL().text
def root = new XmlSlurper().parseText(url)

println "|=Name|=Type|=Location"
root.data."repositories-item".each() {
  println "|${it.name}|${it.repoType}|${it. contentResourceURI}"
}
{{/groovy}}

Note: If you wish you could also use Groovy's HttpBuilder (although it's more complex but the example shows how to use the Grape's @Grab annotation):

{{groovy}}
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1')
import groovyx.net.http.*

def http = new HTTPBuilder( 'http://nexus.xwiki.org/nexus/' )
def resp = http.get( path: 'service/local/repositories')

println "|=Name|=Type|=Location"
resp.data."repositories-item".each() {
  println "|${it.name}|${it.repoType}|${it. contentResourceURI}"
}
{{/groovy}}

Result

nexus-repos.png

List all artifacts matching a GroupId and Version in a specific repository

{{groovy}}
import groovy.xml.*

def url = "http://nexus.xwiki.org/nexus/service/local/data_index?g=org.xwiki.commons&v=3.2-milestone-3".toURL().text
def root = new XmlSlurper().parseText(url)

println "|=GroupId|=ArtifactId|=Version|=Packaging|=Classifier"
root.data.artifact.findAll { it.repoId == "releases" }.each {
  println "|${it.groupId}|${it.artifactId}|${it. version}|${it.packaging}|${it.classifier}"
}
{{/groovy}}

Result

nexus-search.png

Find all files matching a GroupId and Version in a specific repository

{{groovy}}
import groovy.xml.*

def url = "http://nexus.xwiki.org/nexus/service/local/data_index?g=org.xwiki.commons&v=3.2-milestone-3".toURL().text
def root = new XmlSlurper().parseText(url)

println "|=Files"

// Find all main artifacts
root.data.artifact.findAll { it.repoId == "releases" && it.classifier == "" }.each { artifact ->
 // Find all files for the groupid/version
  url = "http://nexus.xwiki.org/nexus/service/local/repositories/releases/content/org/xwiki/commons/${artifact.artifactId}/3.2-milestone-3".toURL().text
 def files = new XmlSlurper().parseText(url)
  files.data."content-item".each { file ->
    println "|${file.resourceURI}"
  }
}
{{/groovy}}

Result

nexus-files.png

Find all Javadocs

{{groovy}}
import groovy.xml.*
import org.apache.commons.lang3.*

def offset = 0
def count = 200
def shouldContinue = true

while (shouldContinue) {
 def url = "http://nexus.xwiki.org/nexus/service/local/lucene/search?g=org.xwiki.*&v=6.4.2&p=jar&c=javadoc&from=${offset}&count=${count}".toURL().text
 def root = new XmlSlurper().parseText(url)

  offset += count
 def total = Integer.parseInt(root.totalCount.text())
  shouldContinue = total > offset ? true : false

  root.data.artifact.each { artifact ->
   // Extract the last part of the group id and consider it's the repo short name
   def groupId = artifact.groupId.text()
   def repo = StringUtils.substringAfterLast(groupId, ".")
   def artifactId = artifact.artifactId
   def version = artifact.version
   def javadocURL = "http://nexus.xwiki.org/nexus/service/local/repositories/releases/archive/org/xwiki/${repo}/${artifactId}/${version}/${artifactId}-${version}-javadoc.jar/!/index.html"
    println "* [[${artifactId}>>url:${javadocURL}]]"
  }
}
{{/groovy}}

Results

nexus-javadoc

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Created by Vincent Massol on 2011/08/22 23:00
     

Get Connected