Honor selected JDK during Maven detection

This commit is contained in:
王鹏
2026-10-09 12:23:10 +08:00
parent 2f3d275021
commit d524d160f6
4 changed files with 117 additions and 11 deletions

View File

@@ -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)