Created
November 18, 2010 17:11
-
-
Save adamv/705292 to your computer and use it in GitHub Desktop.
Uploading to Nexus using Maven from the command line (in Python). Requires Maven and Curl.
This file contains 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
def local2(command, print_command=False): | |
"Run a command, returning the exit code, output, and stderr." | |
from subprocess import Popen, PIPE | |
p = Popen(command, stdout=PIPE, stderr=PIPE) | |
if print_command: print " ".join(command) | |
output, errput = p.communicate() | |
return p.returncode, output, errput | |
class NexusRepository(object): | |
def __init__(self, hostname, repository=None): | |
if repository is None: | |
repository = "releases" | |
self.hostname = hostname | |
self.repository = repository | |
def _determine_packaging(self, filename, packaging): | |
if packaging is not None: | |
return packaging | |
if filename.endswith('.tar.gz'): | |
return 'tar.gz' | |
return os.path.splitext(filename)[1].lstrip('.') | |
def upload(self, artifact, sourcefile, packaging=None): | |
if artifact.version in [None, 'LATEST']: | |
raise Exception("Cannot upload version 'LATEST'.") | |
url = "http://%(hostname)s/content/repositories/%(repo)s" % { | |
'hostname': self.hostname, | |
'repo': self.repository, | |
} | |
if packaging is None: | |
if sourcefile.endswith('.tar.gz'): | |
packaging = 'tar.gz' | |
else: | |
packaging = os.path.splitext(sourcefile)[1].lstrip('.') | |
status, stdout, stderr = local2([ | |
MAVEN_BINARY, | |
"deploy:deploy-file", | |
"-Durl=" + url, | |
"-DrepositoryId=" + self.repository, | |
"-Dversion=" + artifact.version, | |
"-Dfile=" + sourcefile, | |
"-DartifactId=" + artifact.artifact_id, | |
"-Dpackaging=" + self._determine_packaging(sourcefile, packaging), | |
"-DgroupId=" + artifact.group_id, | |
]) | |
return status, stdout, stderr | |
def download(self, artifact, targetfile=None, packaging=None): | |
params = { | |
'r': self.repository, | |
'g': artifact.group_id, | |
'a': artifact.artifact_id, | |
'v': artifact.version | |
} | |
params['e'] = self._determine_packaging(targetfile, packaging) | |
if artifact.classifier: | |
params['c'] = artifact.classifier | |
url = "http://%(hostname)s/service/local/artifact/maven/redirect?%(qs)s" % { | |
'hostname': self.hostname, | |
'qs': urllib.urlencode(params), | |
} | |
print "Will try to download from:" | |
print url | |
curl_args = ["curl", "-sSLA", "fabric-deploy", url] | |
if targetfile is not None: | |
curl_args.extend(["-o", targetfile]) | |
else: | |
curl_args.append("-O") | |
status, stdout, stderr = local2(curl_args) | |
return status, stdout, stderr | |
class NexusArtifact(object): | |
def __init__(self, group_id, artifact_id, version=None, classifier=None): | |
if version is None: | |
version = "LATEST" | |
self.group_id = group_id | |
self.artifact_id = artifact_id | |
self.version = version | |
self.classifier = classifier |
argh. I wrote too fast. maven deploy plugin already has an option to specify the pom. ignore my previous post :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice script. I came across while trying to figure out how to automate the upload of artifacts to a nexus repository. Sometimes I get requests from developers to upload jars to our internal nexus repository and they provide me a directory with multiple pom and jar files. Uploading each one at a time takes quite a while to do.
FYI, the developers generate this directory via the following command:
mvn dependency:copy-dependencies -Dmdep.useSubDirectoryPerArtifact=true -Dmdep.copyPom=true -DoutputDirectory=c:\temp\hydra\dependencies -s settings.xml
Since they are giving me the pom for every artifact, I can easily extract all the required information (-Dfile, -DartifactId, -Dpackaging, -DgroupId...) with python's xml minidom, and simply call your script to upload all the files.
I'm not using your script, but I get the gist of what you're doing. Good job.