Skip to content

Instantly share code, notes, and snippets.

@paul-hammant
Last active October 21, 2019 10:21
Show Gist options
  • Save paul-hammant/581b0293f8597e57b1b305d6e6a63162 to your computer and use it in GitHub Desktop.
Save paul-hammant/581b0293f8597e57b1b305d6e6a63162 to your computer and use it in GitHub Desktop.
Generate quickest Maven script for the pending changes in a Git checkout
#!/bin/python3
# See https://paulhammant.com/2019/10/20/quicker-local-maven-builds
import sh, os
from pathlib import Path
log = sh.git.log("--oneline", "--no-color", "--decorate=short", _tty_out=False)
hashLine = ""
for line in log.split("\n"):
if ' (origin/master,' in line:
hashLine = line
break
if 'master, origin/master' in line:
hashLine = line
break
if (len(hashLine) is 0):
print("Could not determine origin/master SHA1")
exit(1)
originHash = hashLine.split(" ")[0]
diffFiles = sh.git.diff("--name-status", originHash, _tty_out=False)
allModules = []
for filename in Path('.').glob('**/pom.xml'):
allModules.append(str(filename).split("/pom.xml")[0])
allModulesWithSourceTrees = []
for filename in Path('.').glob('**/src/main'):
srcDir = str(filename).split("/src/main")[0]
if srcDir in allModules and srcDir not in allModulesWithSourceTrees:
allModulesWithSourceTrees.append(srcDir)
impactedProdModules = []
impactedTestModules = []
for diffFile in diffFiles:
file = diffFile.split("\t")[1].strip()
for m in allModulesWithSourceTrees:
if file.startswith(m) and "src/test/" not in file and m not in impactedProdModules:
impactedProdModules.append(m)
impactedTestModules.append(m)
if file.startswith(m) and "src/test/" in file and m not in impactedTestModules:
impactedTestModules.append(m)
with open(".mvnCommands.sh", 'w') as out:
out.write("#!/bin/sh\n\nset -e\n\n")
out.write("mvn install -DskipTests -pl " + ",".join(impactedProdModules) + "\n\n")
out.write("mvn test -Dmaven.main.skip -pl " + ",".join(impactedTestModules) + "\n\n")
os.chmod(".mvnCommands.sh", 0o775)
print(".mvnCommands.sh updated")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment