Created
July 10, 2012 12:53
-
-
Save tobrien/3083075 to your computer and use it in GitHub Desktop.
Groovy Script to Automate Staging with Nexus Professional
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.json.JsonSlurper | |
import groovy.json.JsonBuilder | |
apply plugin: 'java' | |
apply plugin: 'maven' | |
def repo = "http://localhost:8081/nexus/" | |
task stage_close(type: StagingCloseTask) | |
task stage_list(type: StagingListTask) | |
repositories { | |
maven { | |
url repo + "content/groups/public" | |
} | |
} | |
dependencies { | |
compile "com.example:simple-project:1.0-SNAPSHOT" | |
} | |
uploadArchives { | |
repositories { | |
mavenDeployer { | |
repository(url: repo + "service/local/staging/deploy/maven2") { | |
authentication(userName: "admin", password: "admin123") | |
} | |
pom.version = "1.0" | |
pom.artifactId = "another-project" | |
pom.groupId = "com.example" | |
} | |
} | |
} | |
class StagingListTask extends DefaultTask { | |
def repo = "http://localhost:8081/nexus/" | |
def username = "admin" | |
def password = "admin123" | |
@TaskAction | |
def list() { | |
def listStagingURL = repo + "service/local/staging/profile_repositories" | |
def authString = "${username}:${password}" | |
.getBytes().encodeBase64().toString() | |
def conn = listStagingURL.toURL().openConnection() | |
conn.setRequestProperty("Authorization", "Basic ${authString}") | |
conn.setRequestProperty("Accept", "application/json") | |
if(conn.responseCode == 200){ | |
def profiles = new JsonSlurper().parseText( conn.content.text ) | |
profiles.data.each { | |
println it.repositoryId + ", " + it.type | |
} | |
} else { | |
println "Something bad happened." | |
println "${conn.responseCode}: ${conn.responseMessage}" | |
} | |
} | |
} | |
class StagingCloseTask extends DefaultTask { | |
def repo = "http://localhost:8081/nexus/" | |
def username = "admin" | |
def password = "admin123" | |
def repoId = System.properties['repoId'] | |
@TaskAction | |
def close() { | |
def listStagingURL = repo + | |
"service/local/staging/bulk/close" | |
def authString = "${username}:${password}" | |
.getBytes().encodeBase64().toString() | |
def conn = listStagingURL.toURL().openConnection() | |
conn.setRequestMethod("POST") | |
conn.setRequestProperty("Authorization", "Basic ${authString}") | |
conn.setRequestProperty("Accept", "application/json") | |
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8") | |
conn.setDoOutput( true ) | |
Writer wr = new OutputStreamWriter(conn.outputStream) | |
wr.write("{'data':{'stagedRepositoryIds':['${repoId}'],'description':''}}"); | |
wr.flush() | |
wr.close() | |
conn.connect() | |
if(conn.responseCode == 201){ | |
println "Repository Closed" | |
} else { | |
println "Something bad happened." | |
println "${conn.responseCode}: ${conn.responseMessage}" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment