Created
December 30, 2016 10:56
-
-
Save danekja/c138caad5a7a63c3993a64d150c3b14f to your computer and use it in GitHub Desktop.
Script for bulk deploy of a local Maven repository into a remote one.
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
""" | |
Deploys local maven repository to a remote one. | |
Requires python 3.x and Maven 3.2.x and higher (lower versions of Maven might work in certain setups, | |
but have issues with SNI). | |
""" | |
import os | |
import os.path as p | |
import subprocess as subp | |
TYPE_JAR = "jar" | |
TYPE_WAR = "war" | |
TYPE_POM = "pom" | |
""" | |
Maven command variants | |
""" | |
__MVN_CMD = "mvn-3.3 deploy:deploy-file -Durl={0} -DrepositoryId={1} -Dfile={2} -DpomFile={3} " | |
__MVN_JD_CMD = __MVN_CMD + " -Djavadoc={4} " | |
__MVN_S_CMD = __MVN_CMD + " -Dsources={4} " | |
__MVN_JD_S_CMD = __MVN_JD_CMD + " -Dsources={5} " | |
""" | |
Metadata filename, used to distinguish the artifact root folder. | |
""" | |
__METADATA_FILE="maven-metadata.xml" | |
__FILEPART_SOURCES="sources" | |
__FILEPART_JAVADOC="javadoc" | |
__FILEPART_SNAPSHOT="SNAPSHOT" | |
def __build_filename(artifactId, version, type, qualifier=None): | |
""" | |
Builds artifact filename based on its id, version, packaging type (jar, war) and qualifier (sources, javadoc etc) | |
""" | |
path = artifactId + "-" + version | |
if(qualifier is not None): | |
path += "-" + qualifier | |
return path + "." + type | |
def __build_path(dir, artifactId, version, type, qualifier=None): | |
""" | |
Convenience function to create full path to an artifact based on parent dir (root of group), | |
artifact id, version, packaging type and qualifier. | |
""" | |
return p.join(dir, __build_filename(artifactId, version, type, qualifier)) | |
def build_maven_command(url, repositoryId, artifactId, version, dir, artifactPath, pomFile, withSourcesAndJavadoc): | |
""" | |
Builds executable Maven command which deploys an artifact to the given repository. Builds proper command | |
based on whether sources and javadoc are available. | |
""" | |
if withSourcesAndJavadoc: | |
pathSources = __build_path(dir, artifactId, version, TYPE_JAR, __FILEPART_SOURCES) | |
pathJavadoc = __build_path(dir, artifactId, version, TYPE_JAR, __FILEPART_JAVADOC) | |
hasSources = p.exists(pathSources) | |
hasJavadoc = p.exists(pathJavadoc) | |
if(hasSources and hasJavadoc): | |
return __MVN_JD_S_CMD.format(url, repositoryId, artifactPath, pomFile, pathJavadoc, pathSources) | |
elif hasSources: | |
return __MVN_S_CMD.format(url, repositoryId, artifactPath, pomFile, pathSources) | |
elif hasJavadoc: | |
return __MVN_JD_CMD.format(url, repositoryId, artifactPath, pomFile, pathJavadoc) | |
else: | |
return __MVN_CMD.format(url, repositoryId, artifactPath, pomFile) | |
else: | |
return __MVN_CMD.format(url, repositoryId, artifactPath, pomFile) | |
def deploy_binary(url, repoId, dir, artifactId, version): | |
""" | |
Determines what kind of artifact is to be deployed and executes respective maven command. | |
""" | |
pathJar = __build_path(dir, artifactId, version, TYPE_JAR) | |
pathWar = __build_path(dir, artifactId, version, TYPE_WAR) | |
pathPom = __build_path(dir, artifactId, version, TYPE_POM) | |
if not p.exists(pathPom): | |
return | |
cmd = None | |
if p.exists(pathJar): | |
cmd = build_maven_command(url, repoId, artifactId, version, dir, pathJar, pathPom, True) | |
elif p.exists(pathWar): | |
cmd = build_maven_command(url, repoId, artifactId, version, dir, pathWar, pathPom, True) | |
elif p.exists(pathPom): | |
cmd = build_maven_command(url, repoId, artifactId, version, dir, pathPom, pathPom, False) | |
else: | |
return | |
print(cmd) | |
code = subp.call(cmd, shell=True) | |
if code != 0: | |
print("Error code was {0} for artifact {1}".format(code, artifactId)) | |
def deploy_snapshots(url, repoId, dir, artifactId): | |
""" | |
Determines version from present poms. This is necessary for SNAPSHOT deployment | |
in case version locking is enabled (multiple artifacts per SNAPSHOT version) | |
""" | |
for name in os.listdir(dir): | |
if name.endswith(TYPE_POM): | |
version = name.split(artifactId)[1][1:-4] | |
deploy_binary(url, repoId, dir, artifactId, version) | |
def deploy_artefact_versions(url, repoId, dir, artifactId): | |
""" | |
Deploys all versions of the given artifact. | |
Expects dir to be root of the artifact directory (subdirs are the individual versions) | |
""" | |
for name in os.listdir(dir): | |
artifactDir = p.join(dir, name) | |
if p.isdir(artifactDir): | |
if __FILEPART_SNAPSHOT in name: | |
deploy_snapshots(url, repoId, artifactDir, artifactId) | |
else: | |
deploy_binary(url, repoId, artifactDir, artifactId, name) | |
def deploy_folder(url, repoId, path): | |
""" | |
Deploys all artifacts in the given folder. Expects the path parameter to point to any parent dir of the artifacts, | |
i.e. that there is a child directory which represents the artifactId. | |
The directory representing artifactId is determined based on presence of maven metadata file. | |
""" | |
for name in os.listdir(path): | |
nextdir = p.join(path, name) | |
if p.isdir(nextdir): | |
if p.exists(p.join(nextdir, __METADATA_FILE)): | |
deploy_artefact_versions(url, repoId, nextdir, name) | |
else: | |
deploy_folder(url, repoId, nextdir) | |
def main(args=[]): | |
""" | |
TODO use cmd parameters to make this real :) | |
""" | |
deploy_folder("deploy url", "repoid in settings.xml", "root folder of the repo to deploy") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment