import zipfile, os, subprocess, glob, sys BASE = os.path.dirname(os.path.abspath(__file__)) SRC = os.path.join(BASE, "src", "main", "java") OUT = os.path.join(BASE, "build_tmp_classes") LIB = os.path.join(BASE, "lib") JAVAC = r"D:\Program Files\Java\jdk-17\bin\javac.exe" SRC_JAR = os.path.join(BASE, "target", "resource-platform-v5.jar") DST_JAR = os.path.join(BASE, "target", "resource-platform-v6.jar") # 1) 收集依赖 classpath dep_jars = sorted(glob.glob(os.path.join(LIB, "*.jar"))) cp = ";".join(dep_jars) print("classpath entries:", len(dep_jars)) # 2) 编译全部源码 sources = [] for root, _, files in os.walk(SRC): for f in files: if f.endswith(".java"): sources.append(os.path.join(root, f)) print("source files:", len(sources)) os.makedirs(OUT, exist_ok=True) cmd = [JAVAC, "-parameters", "-encoding", "UTF-8", "-cp", cp, "-d", OUT] + sources r = subprocess.run(cmd, capture_output=True) print("javac returncode:", r.returncode) if r.returncode != 0: err = (r.stderr or b"").decode("utf-8", "replace") print("=== STDERR (first 40 lines) ===") print("\n".join(err.splitlines()[:40])) sys.exit(1) print("compiled classes:", len(glob.glob(os.path.join(OUT, "**", "*.class"), recursive=True))) # 3) 重打包: 以 v5 为基底, 替换 BOOT-INF/classes 下新编译的 class with zipfile.ZipFile(SRC_JAR) as zin: infos = zin.infolist() names = set(zin.namelist()) def class_local(entry_name): # 仅替换真正的 .class 文件; 目录条目(以/结尾)直接沿用原 jar if entry_name.startswith("BOOT-INF/classes/") and entry_name.endswith(".class"): rel = entry_name[len("BOOT-INF/classes/"):] local = os.path.join(OUT, rel) if os.path.isfile(local): return local return None # MANIFEST 必须第一个写入 manifest_name = "META-INF/MANIFEST.MF" with zipfile.ZipFile(DST_JAR, "w", compression=zipfile.ZIP_DEFLATED) as zout: mi = next(i for i in infos if i.filename == manifest_name) zout.writestr(mi, zin.read(manifest_name)) for i in infos: if i.filename == manifest_name: continue local = class_local(i.filename) if local: with open(local, "rb") as lf: data = lf.read() zi = zipfile.ZipInfo(i.filename) zi.external_attr = i.external_attr zi.date_time = i.date_time zout.writestr(zi, data) else: zout.writestr(i, zin.read(i.filename)) print("built:", DST_JAR, "exists:", os.path.exists(DST_JAR)) print("manifest first:", zipfile.ZipFile(DST_JAR).namelist()[0] == manifest_name)