Nexus Integration
Last modified by Vincent Massol on 2025/02/12 12:25
![]() | Uses REST and Groovy to integrate with Nexus |
Type | Snippet |
Category | |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Table of contents
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}}
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}}
@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
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}}
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
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}}
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
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}}
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
Prerequisites & Installation Instructions
- If you're using an XWiki version lower than 3.2 and you want to use Groovy's HttpBuilder
- Download the Ivy JAR and copy it in your WEB-INF/lib directory. This is needed by the Groovy @Grab directive.
- If you're using XWiki 3.2+ then you're good to go!