Pass selected JDK to backend builds

This commit is contained in:
王鹏
2026-10-09 14:44:09 +08:00
parent c2d124cc09
commit 11cec27774
4 changed files with 73 additions and 6 deletions

View File

@@ -47,6 +47,7 @@ class DeploymentStep:
service_name: str | None = None
port: int | None = None
url: str | None = None
environment: Mapping[str, str] | None = None
OutputHandler = Callable[[str], None]
@@ -87,7 +88,16 @@ def build_deployment_plan(scan: ScanResult, config: DeploymentConfig) -> list[De
else:
command = [_tool_command(config, "Maven", "mvn"), "clean", "package", "-DskipTests"]
description = "构建 SSM WAR 应用" if backend_is_ssm else "打包 Spring Boot 应用"
steps.append(DeploymentStep("构建后端", command, backend_dir, description, kind="backend_build"))
steps.append(
DeploymentStep(
"构建后端",
command,
backend_dir,
description,
kind="backend_build",
environment=_java_build_environment(config),
)
)
frontends = [] if config.backend_only else scan.detected_frontends
for frontend in frontends:
@@ -268,6 +278,33 @@ def _required_tool_command(config: DeploymentConfig, tool_name: str, fallback: s
raise FileNotFoundError(f"部署 SSM 项目需要 {tool_name},请先安装或选择其可执行文件")
def _java_build_environment(config: DeploymentConfig) -> dict[str, str]:
candidates: list[Path] = []
configured = config.tool_paths.get("JDK") if config.tool_paths else None
if configured:
resolved = resolve_tool_path("JDK", configured)
if resolved:
candidates.append(resolved)
discovered = discover_executable("JDK", config.tool_search_roots)
if discovered and discovered not in candidates:
candidates.append(discovered)
path_javac = shutil.which("javac")
if path_javac:
candidates.append(Path(path_javac).with_name("java.exe" if os.name == "nt" else "java"))
javac_name = "javac.exe" if os.name == "nt" else "javac"
for java in candidates:
java_bin = java.parent
if not (java_bin / javac_name).is_file():
continue
current_path = os.environ.get("PATH", "")
return {
"JAVA_HOME": str(java_bin.parent),
"PATH": os.pathsep.join(part for part in (str(java_bin), current_path) if part),
}
return {}
def _default_npm_command() -> str:
return "npm.cmd" if os.name == "nt" else "npm"
@@ -768,7 +805,17 @@ def run_steps(
_deploy_ssm_war(source_war, step.output_path, on_output)
return_code = runner(command, step.cwd, on_output, step.input_path, step.background)
else:
return_code = runner(command, step.cwd, on_output, step.input_path, step.background)
if process_runner is None:
return_code = _run_process(
command,
step.cwd,
on_output,
step.input_path,
step.background,
environment=step.environment,
)
else:
return_code = runner(command, step.cwd, on_output, step.input_path, step.background)
except Exception as exc:
on_output(f"步骤失败: {exc}")
return 1
@@ -840,6 +887,7 @@ def _run_process(
on_output: OutputHandler,
input_path: Path | None = None,
background: bool = False,
environment: Mapping[str, str] | None = None,
) -> int:
command = resolve_process_command(command)
if background:
@@ -847,7 +895,7 @@ def _run_process(
input_file = _open_utf8_sql_input(input_path, on_output) if input_path else None
try:
process_environment = _build_process_environment(command)
process_environment = _build_process_environment(command, environment)
process = subprocess.Popen(
command,
cwd=cwd,
@@ -866,8 +914,12 @@ def _run_process(
input_file.close()
def _build_process_environment(command: list[str]) -> dict[str, str]:
def _build_process_environment(
command: list[str], overrides: Mapping[str, str] | None = None
) -> dict[str, str]:
environment = os.environ.copy()
if overrides:
environment.update(overrides)
executable = Path(command[0]) if command else None
if executable and executable.parent != Path("."):
executable_dir = str(executable.parent)