Pass selected JDK to backend builds
This commit is contained in:
@@ -19,6 +19,7 @@ AutoDeploy 是一个面向 Windows 的 Spring Boot / SSM + Vue 本地自动化
|
||||
- Maven、npm 等命令输出会自动识别 UTF-8 和 Windows 中文编码,避免日志解码导致部署任务提前中断。
|
||||
- 使用手动指定或工具自动安装的 npm 时,会把其 Node.js 目录加入依赖安装和前端启动子进程的 `PATH`,保证 npm 生命周期脚本可以调用 `node`。
|
||||
- 前端进程在端口就绪前退出时,主界面会直接显示日志最后 30 行,便于定位编译和配置错误。
|
||||
- Maven 和 Gradle 构建会自动使用工具中选择或发现的完整 JDK,并为构建进程设置 `JAVA_HOME`,无需重启电脑。
|
||||
|
||||
## 运行环境
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -767,6 +804,16 @@ def run_steps(
|
||||
else:
|
||||
_deploy_ssm_war(source_war, step.output_path, on_output)
|
||||
return_code = runner(command, step.cwd, on_output, step.input_path, step.background)
|
||||
else:
|
||||
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:
|
||||
@@ -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)
|
||||
|
||||
@@ -145,7 +145,11 @@ def discover_executable(name: str, search_roots: Iterable[Path] | None = None) -
|
||||
for root in roots:
|
||||
if not root.exists() or not root.is_dir():
|
||||
continue
|
||||
for candidate in _direct_candidates(name, root):
|
||||
candidates = _direct_candidates(name, root)
|
||||
if name == "JDK":
|
||||
javac_name = "javac.exe" if os.name == "nt" else "javac"
|
||||
candidates.sort(key=lambda candidate: not (candidate.parent / javac_name).is_file())
|
||||
for candidate in candidates:
|
||||
if candidate.exists() and candidate.name.lower() in executable_names:
|
||||
return candidate
|
||||
return None
|
||||
|
||||
@@ -36,11 +36,13 @@ class DeployerTests(unittest.TestCase):
|
||||
[str(npm), "install"],
|
||||
Path("C:/project/web"),
|
||||
output.append,
|
||||
environment={"JAVA_HOME": str(Path.cwd() / "fake-jdk")},
|
||||
)
|
||||
|
||||
self.assertEqual(exit_code, 1)
|
||||
self.assertEqual(output, ["npm ERR! ⚠ dependency failed", "安装失败"])
|
||||
self.assertNotIn("text", popen_options)
|
||||
self.assertEqual(popen_options["env"]["JAVA_HOME"], str(Path.cwd() / "fake-jdk"))
|
||||
self.assertEqual(
|
||||
Path(popen_options["env"]["PATH"].split(os.pathsep)[0]),
|
||||
npm.parent,
|
||||
@@ -633,21 +635,29 @@ class DeployerTests(unittest.TestCase):
|
||||
root = Path(temp)
|
||||
backend = root / "server"
|
||||
maven = root / "tools" / "apache-maven" / "bin" / "mvn.cmd"
|
||||
jdk_bin = root / "tools" / "jdk8" / "bin"
|
||||
java = jdk_bin / "java.exe"
|
||||
javac = jdk_bin / "javac.exe"
|
||||
backend.mkdir()
|
||||
maven.parent.mkdir(parents=True)
|
||||
jdk_bin.mkdir(parents=True)
|
||||
(backend / "pom.xml").write_text("<project />", encoding="utf-8")
|
||||
maven.write_text("", encoding="utf-8")
|
||||
java.write_text("", encoding="utf-8")
|
||||
javac.write_text("", encoding="utf-8")
|
||||
scan = ScanResult(
|
||||
root=root,
|
||||
backend=ProjectComponent("Spring Boot", backend, "Maven project"),
|
||||
frontend=None,
|
||||
sql_files=[],
|
||||
)
|
||||
config = DeploymentConfig(tool_paths={"Maven": maven})
|
||||
config = DeploymentConfig(tool_paths={"Maven": maven, "JDK": java})
|
||||
|
||||
plan = build_deployment_plan(scan, config)
|
||||
|
||||
self.assertEqual(plan[0].command, [str(maven), "clean", "package", "-DskipTests"])
|
||||
self.assertEqual(plan[0].environment["JAVA_HOME"], str(jdk_bin.parent))
|
||||
self.assertEqual(Path(plan[0].environment["PATH"].split(os.pathsep)[0]), jdk_bin)
|
||||
|
||||
def test_deploys_ssm_war_to_configured_tomcat(self):
|
||||
with TemporaryDirectory() as temp:
|
||||
|
||||
Reference in New Issue
Block a user