103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
import xml.etree.ElementTree as ET
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
def install_maven(maven_version="3.9.6"):
|
|
"""Install Maven dynamically if not found"""
|
|
try:
|
|
subprocess.run(["mvn", "--version"], check=True, capture_output=True)
|
|
print("✓ Maven already installed")
|
|
except (subprocess.CalledProcessError, FileNotFoundError):
|
|
print("⏳ Installing Maven...")
|
|
maven_url = f"https://archive.apache.org/dist/maven/maven-3/{maven_version}/binaries/apache-maven-{maven_version}-bin.tar.gz"
|
|
install_cmds = [
|
|
f"curl -sL {maven_url} | tar xz -C /opt",
|
|
f"ln -s /opt/apache-maven-{maven_version}/bin/mvn /usr/local/bin/mvn",
|
|
"rm -rf /tmp/*"
|
|
]
|
|
for cmd in install_cmds:
|
|
subprocess.run(cmd, shell=True, check=True)
|
|
print("✓ Maven installed")
|
|
|
|
def install_java(java_version="21"):
|
|
"""Install JDK dynamically if not found"""
|
|
try:
|
|
jdk_check = subprocess.run(["java", "--version"], capture_output=True, text=True)
|
|
if f" {java_version}" in jdk_check.stdout:
|
|
print(f"✓ JDK {java_version} already installed")
|
|
return
|
|
except FileNotFoundError:
|
|
pass
|
|
|
|
print(f"⏳ Installing JDK {java_version}...")
|
|
subprocess.run([
|
|
"apt-get", "update", "-qq"
|
|
], check=True)
|
|
|
|
subprocess.run([
|
|
"apt-get", "install", "-qq", "-y",
|
|
f"temurin-{java_version}-jdk"
|
|
], check=True)
|
|
print(f"✓ JDK {java_version} installed")
|
|
|
|
def analyze_pom(pom_path):
|
|
"""Detect required tools from pom.xml"""
|
|
tree = ET.parse(pom_path)
|
|
root = tree.getroot()
|
|
ns = {'mvn': 'http://maven.apache.org/POM/4.0.0'}
|
|
|
|
requirements = {
|
|
'java_version': root.findtext('mvn:properties/mvn:java.version', namespaces=ns, default="21"),
|
|
'kotlin_version': root.findtext('mvn:properties/mvn:kotlin.version', namespaces=ns),
|
|
'needs_docker': False
|
|
}
|
|
|
|
# Check for docker-maven-plugin
|
|
for plugin in root.findall('.//mvn:plugin', namespaces=ns):
|
|
if 'docker' in plugin.findtext('mvn:artifactId', namespaces=ns, default=""):
|
|
requirements['needs_docker'] = True
|
|
break
|
|
|
|
return requirements
|
|
|
|
def optimize_build(pom_path):
|
|
"""Apply build optimizations based on project type"""
|
|
optimizations = [
|
|
"-T 1C", # Parallel builds (1 thread per core)
|
|
"-Ddependency.cache=true",
|
|
"-Dmaven.artifact.threads=8"
|
|
]
|
|
|
|
if "kotlin" in pom_path.read_text():
|
|
optimizations.append("-Dkotlin.compiler.incremental=true")
|
|
|
|
return " ".join(optimizations)
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description='Smart Maven project builder')
|
|
parser.add_argument('pom_path', type=str, help='Path to pom.xml')
|
|
parser.add_argument('--skip-install', action='store_true', help='Skip dependency installation')
|
|
args = parser.parse_args()
|
|
|
|
pom_path = Path(args.pom_path)
|
|
requirements = analyze_pom(pom_path)
|
|
|
|
if not args.skip_install:
|
|
install_java(requirements['java_version'])
|
|
install_maven()
|
|
|
|
build_cmd = f"mvn -B {optimize_build(pom_path)} clean package"
|
|
print(f"🚀 Build command: {build_cmd}")
|
|
|
|
try:
|
|
subprocess.run(build_cmd, shell=True, check=True)
|
|
print("✅ Build succeeded!")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ Build failed: {e}")
|
|
sys.exit(1)
|
|
|
|
if __name__ == "__main__":
|
|
main() |