Skip to content

Instantly share code, notes, and snippets.

@ypujante
Created November 21, 2010 18:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ypujante/708980 to your computer and use it in GitHub Desktop.
Save ypujante/708980 to your computer and use it in GitHub Desktop.
This script uploads all your artifacts to maven (staging)
/*
* Copyright (c) 2010-2010 LinkedIn, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* This script uploads all your artifacts to maven.
*
* To run this script there are several asumptions:
* - gpg is installed and available in the path
* - you have followed the instructions to set up your pgp key
* (http://www.sonatype.com/people/2010/01/how-to-generate-pgp-signatures-with-maven/)
* - maven is installed and available in the path
* - you have defined the proper server section in your ~/.m2/settings.xml file:
* <settings>
* ...
* <servers>
* <server>
* <id>nexus-releases</id>
* <username>ypujante</username>
* <password>********</password>
* </server>
* </servers>
* ...
* </settings>
*
* You run the script this way: groovy maven_staging_upload.groovy pathToPomFile1 pathToPomFile2...
*
* groovy
*/
/**
* Forks a process to execute the command line provided (as a single string) and returns a map
* with 'res' being the return value from the process, 'output' being the output of the process
* as a byte[] and 'error' being the error stream as byte array
*/
def forkAndExec2(char[] passphrase, String commandLine)
{
def pb = new ProcessBuilder(['bash', '-c', commandLine])
Process process = pb.start()
if(passphrase)
{
process.outputStream << passphrase
process.outputStream.close()
}
def out = new ByteArrayOutputStream()
def outThread = Thread.start(commandLine) {
out << new BufferedInputStream(process.inputStream)
}
def err = new ByteArrayOutputStream()
def errThread = Thread.start(commandLine) {
err << new BufferedInputStream(process.errorStream)
}
// we wait for the process to be done
def res = process.waitFor()
// we wait for the threads to have finished reading the output and error
outThread.join()
errThread.join()
def output = out.toByteArray()
def error = err.toByteArray()
return [res: res, output: output, error: error]
}
/**
* Converts the output into a string. Assumes that the encoding is UTF-8. Replaces all line feeds
* by '\n' and remove the last line feed.
*/
private def toStringOutput(byte[] output)
{
return output ? new String(output, "UTF-8").readLines().join('\n') : ""
}
_passphrase = [] as char[]
char[] getPassphrase()
{
if(!this._passphrase)
{
def console = System.console()
_passphrase = console.readPassword("Enter passphrase: ")
}
return _passphrase
}
def pgpSign(File f)
{
def signedFile = new File(f.parentFile, "${f.name}.asc")
signedFile.delete()
def result = forkAndExec2(passphrase, "gpg -ab --batch --passphrase-fd 0 ${f.canonicalPath}")
if(result.res != 0)
throw new RuntimeException(toStringOutput(result.error))
return signedFile
}
filesToBundleFilter = { f, n ->
if(new File(f, n).isDirectory())
return false
!(n.endsWith('.asc') || n.endsWith('.sha1') || n.endsWith('.md5'))
} as FilenameFilter
def mavenStagingUpload(File pomFile)
{
def allFiles = pomFile.parentFile.listFiles(filesToBundleFilter) as List
allFiles.collect { it }.each { File f ->
allFiles << pgpSign(f)
}
def baseName = pomFile.name - '.pom'
println "####### ${baseName} #######"
allFiles.each { file ->
def name = file.name - baseName
def idx = name.indexOf('.')
def classifier = name[0..<idx]
if(classifier)
{
classifier = classifier - '-'
}
def packaging = name[idx+1..-1]
def cmdline = []
cmdline << 'mvn'
cmdline << 'deploy:deploy-file'
cmdline << '-Durl=https://oss.sonatype.org/service/local/staging/deploy/maven2/'
cmdline << '-DrepositoryId=nexus-releases'
cmdline << "-Dfile=${file.canonicalPath}"
cmdline << "-DpomFile=${pomFile.canonicalPath}"
cmdline << "-Dpackaging=${packaging}"
if(classifier)
cmdline << "-Dclassifier=${classifier}"
println "...Uploading ${file.name}..."
def result = forkAndExec2(null, cmdline.join(' '))
if(result.res != 0)
throw new RuntimeException(toStringOutput(result.error))
println toStringOutput(result.output)
}
}
def cli = new CliBuilder(usage: 'maven_staging_upload [-r <repositoryId>] pomFil1 [pomFil2...]')
cli.r(longOpt: 'repositoryId', 'repository id (from settings.xml)', args:1, required: false)
def options = cli.parse(args)
if(!options)
{
return
}
def poms = options.arguments().collect { new File(it) }
poms.each { File pom ->
mavenStagingUpload(pom)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment