mirror of
https://github.com/hpd840321/starRiverProperty.git
synced 2026-06-09 16:30:29 +08:00
Initial commit: five Maven reactors and docs only
Track maven-cloudwalk-cloud, maven-cw-elevator-application, maven-intelligent-cwoscomponent, maven-ninca-crk, maven-ninca-qk-alarm, and docs/. Other workspace paths ignored via .gitignore. Made-with: Cursor
This commit is contained in:
+150
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
将 cw-elevator-application-V1.0.0.20211103/lib 下全部 JAR 作为初始构件上传到 Nexus。
|
||||
|
||||
脚本位置:docs/operations/deploy_cw_elevator_v1_lib_to_nexus.py(与文档同目录,便于运维查找)。
|
||||
|
||||
规则:
|
||||
1. 若 JAR 内含 META-INF/maven/**/pom.properties,则使用其中的 groupId / artifactId / version。
|
||||
2. 否则使用后备坐标:cn.cloudwalk.elevator.v1.bootlib:<文件名去 .jar>:V1.0.0.20211103
|
||||
(保证与 Maven Central 坐标不冲突,且一文件一构件。)
|
||||
|
||||
version 含 SNAPSHOT 时上传到 maven-snapshots,否则 maven-releases。
|
||||
需 ~/.m2/settings.xml 中配置 server id:nexus-releases、nexus-snapshots(与 URL 对应)。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
DEFAULT_LIB = Path(
|
||||
"/media/zebra/9e8fa357-7db6-4d70-88ed-d5de5a059a663/星河湾星中星/反编译"
|
||||
"/cw-elevator-application-V1.0.0.20211103/lib"
|
||||
)
|
||||
RELEASES_URL = "http://192.168.3.12:8081/repository/maven-releases/"
|
||||
SNAPSHOTS_URL = "http://192.168.3.12:8081/repository/maven-snapshots/"
|
||||
RELEASES_ID = "nexus-releases"
|
||||
SNAPSHOTS_ID = "nexus-snapshots"
|
||||
FALLBACK_GROUP = "cn.cloudwalk.elevator.v1.bootlib"
|
||||
FALLBACK_VERSION = "V1.0.0.20211103"
|
||||
|
||||
|
||||
def read_maven_coords(jar: Path) -> tuple[str, str, str] | None:
|
||||
try:
|
||||
with zipfile.ZipFile(jar, "r") as zf:
|
||||
for name in zf.namelist():
|
||||
if name.endswith("pom.properties") and "META-INF/maven" in name.replace(
|
||||
"\\", "/"
|
||||
):
|
||||
raw = zf.read(name).decode("utf-8", errors="replace")
|
||||
props: dict[str, str] = {}
|
||||
for line in raw.splitlines():
|
||||
line = line.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
k, _, v = line.partition("=")
|
||||
props[k.strip()] = v.strip()
|
||||
gid = props.get("groupId")
|
||||
aid = props.get("artifactId")
|
||||
ver = props.get("version")
|
||||
if gid and aid and ver:
|
||||
return gid, aid, ver
|
||||
except (zipfile.BadZipFile, OSError):
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def fallback_coords(jar: Path) -> tuple[str, str, str]:
|
||||
stem = jar.name[:-4] if jar.name.lower().endswith(".jar") else jar.name
|
||||
return FALLBACK_GROUP, stem, FALLBACK_VERSION
|
||||
|
||||
|
||||
def is_snapshot_version(version: str) -> bool:
|
||||
return "SNAPSHOT" in version.upper()
|
||||
|
||||
|
||||
def deploy_one(
|
||||
jar: Path,
|
||||
group_id: str,
|
||||
artifact_id: str,
|
||||
version: str,
|
||||
dry_run: bool,
|
||||
) -> int:
|
||||
url = SNAPSHOTS_URL if is_snapshot_version(version) else RELEASES_URL
|
||||
rid = SNAPSHOTS_ID if is_snapshot_version(version) else RELEASES_ID
|
||||
cmd = [
|
||||
"mvn",
|
||||
"-q",
|
||||
"deploy:deploy-file",
|
||||
f"-DrepositoryId={rid}",
|
||||
f"-Durl={url}",
|
||||
f"-Dfile={jar}",
|
||||
f"-DgroupId={group_id}",
|
||||
f"-DartifactId={artifact_id}",
|
||||
f"-Dversion={version}",
|
||||
"-Dpackaging=jar",
|
||||
"-DgeneratePom=true",
|
||||
]
|
||||
if dry_run:
|
||||
print("DRY-RUN:", " ".join(cmd))
|
||||
return 0
|
||||
r = subprocess.run(cmd, capture_output=True, text=True)
|
||||
if r.returncode != 0:
|
||||
combined = (r.stdout or "") + (r.stderr or "")
|
||||
# Nexus maven-releases 对已存在 release 坐标禁止覆盖
|
||||
if "cannot be updated" in combined:
|
||||
print(f"SKIP (already in repo): {jar.name} -> {group_id}:{artifact_id}:{version}")
|
||||
return 0
|
||||
sys.stderr.write(f"FAIL {jar.name} -> {group_id}:{artifact_id}:{version}\n")
|
||||
if r.stdout:
|
||||
sys.stderr.write(r.stdout)
|
||||
if r.stderr:
|
||||
sys.stderr.write(r.stderr)
|
||||
return r.returncode
|
||||
|
||||
|
||||
def main() -> int:
|
||||
p = argparse.ArgumentParser(description="Deploy all JARs from elevator V1 lib to Nexus.")
|
||||
p.add_argument(
|
||||
"--lib",
|
||||
type=Path,
|
||||
default=DEFAULT_LIB,
|
||||
help="Directory containing .jar files",
|
||||
)
|
||||
p.add_argument("--dry-run", action="store_true", help="Print mvn commands only")
|
||||
args = p.parse_args()
|
||||
lib: Path = args.lib
|
||||
if not lib.is_dir():
|
||||
print("Not a directory:", lib, file=sys.stderr)
|
||||
return 2
|
||||
jars = sorted(lib.glob("*.jar"))
|
||||
if not jars:
|
||||
print("No JAR files in", lib, file=sys.stderr)
|
||||
return 2
|
||||
ok, fail = 0, 0
|
||||
for i, jar in enumerate(jars, 1):
|
||||
coords = read_maven_coords(jar)
|
||||
if coords:
|
||||
gid, aid, ver = coords
|
||||
src = "embedded"
|
||||
else:
|
||||
gid, aid, ver = fallback_coords(jar)
|
||||
src = "fallback"
|
||||
print(f"[{i}/{len(jars)}] {jar.name} -> {gid}:{aid}:{ver} ({src})")
|
||||
rc = deploy_one(jar, gid, aid, ver, args.dry_run)
|
||||
if rc == 0:
|
||||
ok += 1
|
||||
else:
|
||||
fail += 1
|
||||
print(f"Done. ok={ok} fail={fail}")
|
||||
return 0 if fail == 0 else 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user