Honor selected JDK during Maven detection
This commit is contained in:
@@ -38,7 +38,7 @@ python main.py
|
||||
|
||||
1. 选择包含后端、前端和 SQL 文件的项目根目录。
|
||||
2. 点击“扫描项目”,确认识别到的组件和推荐 JDK 版本。
|
||||
3. 点击“检测环境”;缺失的工具可以通过“安装环境”处理,也可以手动选择可执行文件。
|
||||
3. 点击“检测环境”;缺失的工具可以通过“安装环境”处理,也可以手动选择可执行文件。Maven 手动路径支持 Maven 根目录、`bin` 目录或 `bin\mvn.cmd`,检测时会使用已选择的 JDK 设置 `JAVA_HOME`。
|
||||
4. 如果需要导入 SQL,填写 MySQL 用户、密码和数据库名。数据库名默认取第一个 SQL 文件的文件名。MySQL 可在“安装环境”中自动安装;首次使用时填写安装过程中配置的 root 密码。
|
||||
5. 如果只需要更新服务端,勾选“只部署后端(保留 SQL 导入)”;程序会跳过所有前端依赖安装、构建和启动步骤。
|
||||
6. 点击“执行部署”,在运行日志中查看每个步骤和最终访问地址。
|
||||
|
||||
@@ -15,7 +15,7 @@ from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Callable, Iterable, Mapping
|
||||
|
||||
from .environment import discover_executable
|
||||
from .environment import discover_executable, resolve_tool_path
|
||||
from .scanner import ScanResult
|
||||
|
||||
|
||||
@@ -249,8 +249,8 @@ def build_deployment_plan(scan: ScanResult, config: DeploymentConfig) -> list[De
|
||||
def _tool_command(config: DeploymentConfig, tool_name: str, fallback: str) -> str:
|
||||
configured = config.tool_paths.get(tool_name) if config.tool_paths else None
|
||||
if configured:
|
||||
path = Path(configured)
|
||||
if path.exists():
|
||||
path = resolve_tool_path(tool_name, configured)
|
||||
if path:
|
||||
return str(path)
|
||||
|
||||
discovered = discover_executable(tool_name, config.tool_search_roots)
|
||||
|
||||
@@ -55,14 +55,46 @@ def check_environment(
|
||||
manual_paths: dict[str, str | Path] | None = None,
|
||||
search_roots: Iterable[Path] | None = None,
|
||||
) -> list[EnvironmentCheck]:
|
||||
command_runner = runner or _run_command
|
||||
selected_checks = checks or DEFAULT_CHECKS
|
||||
configured_paths = {name: Path(value) for name, value in (manual_paths or {}).items() if str(value).strip()}
|
||||
configured_paths: dict[str, Path] = {}
|
||||
invalid_configured_paths: dict[str, Path] = {}
|
||||
for name, value in (manual_paths or {}).items():
|
||||
if not str(value).strip():
|
||||
continue
|
||||
selected = Path(value)
|
||||
resolved = resolve_tool_path(name, selected)
|
||||
if resolved:
|
||||
configured_paths[name] = resolved
|
||||
else:
|
||||
invalid_configured_paths[name] = selected
|
||||
|
||||
command_environment = os.environ.copy()
|
||||
configured_jdk = configured_paths.get("JDK")
|
||||
if configured_jdk:
|
||||
command_environment["JAVA_HOME"] = str(configured_jdk.parent.parent)
|
||||
command_environment["PATH"] = os.pathsep.join(
|
||||
[str(configured_jdk.parent), command_environment.get("PATH", "")]
|
||||
)
|
||||
command_runner = runner or (lambda command: _run_command(command, command_environment))
|
||||
roots = list(search_roots) if search_roots is not None else default_search_roots()
|
||||
results: list[EnvironmentCheck] = []
|
||||
|
||||
for name, command in selected_checks.items():
|
||||
invalid_path = invalid_configured_paths.get(name)
|
||||
if invalid_path:
|
||||
results.append(
|
||||
EnvironmentCheck(
|
||||
name,
|
||||
[str(invalid_path), *command[1:]],
|
||||
False,
|
||||
"",
|
||||
f"手动指定路径无效,未找到 {', '.join(EXECUTABLE_NAMES.get(name, []))}",
|
||||
"手动指定",
|
||||
)
|
||||
)
|
||||
continue
|
||||
manual_path = configured_paths.get(name)
|
||||
manual_result = None
|
||||
if manual_path and manual_path.exists():
|
||||
manual_command = [str(manual_path), *command[1:]]
|
||||
manual_result = _try_command(name, manual_command, "手动指定", command_runner)
|
||||
@@ -83,17 +115,34 @@ def check_environment(
|
||||
results.append(discovered_result)
|
||||
continue
|
||||
|
||||
results.append(path_result)
|
||||
results.append(manual_result or path_result)
|
||||
|
||||
return results
|
||||
|
||||
|
||||
def resolve_tool_path(name: str, selected: str | Path) -> Path | None:
|
||||
path = Path(selected).expanduser()
|
||||
executable_names = {item.lower() for item in EXECUTABLE_NAMES.get(name, [])}
|
||||
if path.is_file():
|
||||
return path if path.name.lower() in executable_names else None
|
||||
if not path.is_dir():
|
||||
return None
|
||||
|
||||
direct = [path / executable for executable in EXECUTABLE_NAMES.get(name, [])]
|
||||
bin_direct = [path / "bin" / executable for executable in EXECUTABLE_NAMES.get(name, [])]
|
||||
for candidate in [*direct, *bin_direct, *_direct_candidates(name, path)]:
|
||||
if candidate.is_file() and candidate.name.lower() in executable_names:
|
||||
return candidate
|
||||
return None
|
||||
|
||||
|
||||
def discover_executable(name: str, search_roots: Iterable[Path] | None = None) -> Path | None:
|
||||
executable_names = {item.lower() for item in EXECUTABLE_NAMES.get(name, [])}
|
||||
if not executable_names:
|
||||
return None
|
||||
|
||||
for root in search_roots or default_search_roots():
|
||||
roots = search_roots if search_roots is not None else default_search_roots()
|
||||
for root in roots:
|
||||
if not root.exists() or not root.is_dir():
|
||||
continue
|
||||
for candidate in _direct_candidates(name, root):
|
||||
@@ -147,13 +196,17 @@ def _try_command(name: str, command: list[str], source: str, runner: CommandRunn
|
||||
output = _combine_output(completed)
|
||||
installed = completed.returncode == 0
|
||||
message = "已安装" if installed else f"命令返回错误码 {completed.returncode}"
|
||||
if not installed and output:
|
||||
message += f": {output}"
|
||||
if installed and source != "PATH":
|
||||
message = f"已安装,来源: {source}"
|
||||
return EnvironmentCheck(name, command, installed, output, message, source)
|
||||
|
||||
|
||||
def _run_command(command: list[str]) -> subprocess.CompletedProcess[str]:
|
||||
environment = os.environ.copy()
|
||||
def _run_command(
|
||||
command: list[str], environment: dict[str, str] | None = None
|
||||
) -> subprocess.CompletedProcess[str]:
|
||||
environment = dict(environment or os.environ)
|
||||
executable = Path(command[0])
|
||||
if executable.name.lower() in {"catalina.bat", "catalina.sh"} and executable.exists():
|
||||
environment["CATALINA_HOME"] = str(executable.parent.parent)
|
||||
|
||||
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import deploy_tool.environment as environment_module
|
||||
from deploy_tool.environment import check_environment, load_tool_paths, save_tool_paths
|
||||
from deploy_tool.environment import check_environment, load_tool_paths, resolve_tool_path, save_tool_paths
|
||||
|
||||
|
||||
class EnvironmentTests(unittest.TestCase):
|
||||
@@ -69,6 +69,59 @@ class EnvironmentTests(unittest.TestCase):
|
||||
self.assertEqual(results[0].command[0], str(node))
|
||||
self.assertEqual(results[0].version, "v16.20.2")
|
||||
|
||||
def test_resolves_maven_root_bin_directory_and_command_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp) / "apache-maven-3.10.0"
|
||||
command = root / "bin" / "mvn.cmd"
|
||||
command.parent.mkdir(parents=True)
|
||||
command.write_text("@echo off", encoding="utf-8")
|
||||
|
||||
self.assertEqual(resolve_tool_path("Maven", root), command)
|
||||
self.assertEqual(resolve_tool_path("Maven", command.parent), command)
|
||||
self.assertEqual(resolve_tool_path("Maven", command), command)
|
||||
|
||||
def test_manual_maven_uses_selected_jdk_as_java_home(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
java = root / "jdk-8" / "bin" / "java.exe"
|
||||
maven = root / "apache-maven" / "bin" / "mvn.cmd"
|
||||
java.parent.mkdir(parents=True)
|
||||
maven.parent.mkdir(parents=True)
|
||||
java.write_text("", encoding="utf-8")
|
||||
maven.write_text("", encoding="utf-8")
|
||||
completed = subprocess.CompletedProcess([str(maven), "-version"], 0, "Maven 3.10.0", "")
|
||||
|
||||
with patch("deploy_tool.environment.subprocess.run", return_value=completed) as run:
|
||||
results = check_environment(
|
||||
checks={"Maven": ["mvn", "-version"]},
|
||||
manual_paths={"JDK": java, "Maven": maven},
|
||||
)
|
||||
|
||||
self.assertTrue(results[0].installed)
|
||||
self.assertEqual(run.call_args.kwargs["env"]["JAVA_HOME"], str(java.parent.parent))
|
||||
self.assertTrue(run.call_args.kwargs["env"]["PATH"].startswith(str(java.parent)))
|
||||
|
||||
def test_manual_maven_failure_is_not_hidden_by_path_failure(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
maven = Path(tmp) / "mvn.cmd"
|
||||
maven.write_text("", encoding="utf-8")
|
||||
|
||||
def runner(command):
|
||||
if command[0] == str(maven):
|
||||
return subprocess.CompletedProcess(command, 1, "", "JAVA_HOME is not defined correctly")
|
||||
raise FileNotFoundError(command[0])
|
||||
|
||||
result = check_environment(
|
||||
runner=runner,
|
||||
checks={"Maven": ["mvn", "-version"]},
|
||||
manual_paths={"Maven": maven},
|
||||
search_roots=[],
|
||||
)[0]
|
||||
|
||||
self.assertFalse(result.installed)
|
||||
self.assertEqual(result.source, "手动指定")
|
||||
self.assertIn("JAVA_HOME", result.message)
|
||||
|
||||
def test_discovers_executable_from_known_search_paths(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
mysql = Path(tmp) / "MySQL" / "MySQL Server 8.4" / "bin" / "mysql.exe"
|
||||
|
||||
Reference in New Issue
Block a user