66import subprocess
77from typing import TYPE_CHECKING , Union
88
9+ import cjdk
910import jpype
1011
1112if TYPE_CHECKING :
1819_DEFAULT_JAVA_VERSION = "11"
1920
2021
21- def ensure_jvm_available (raise_on_error : bool = True ) -> None :
22- """Ensure that the JVM is available, or raise if `raise_on_error` is True ."""
22+ def ensure_jvm_available () -> None :
23+ """Ensure that the JVM is available and Maven is installed ."""
2324 if not is_jvm_available ():
24- cjdk_fetch_java (raise_on_error = raise_on_error )
25+ cjdk_fetch_java ()
2526 if not shutil .which ("mvn" ):
26- cjdk_fetch_maven (raise_on_error = raise_on_error )
27+ cjdk_fetch_maven ()
2728
2829
2930def is_jvm_available () -> bool :
@@ -46,20 +47,8 @@ def _silent_check_output(*args, **kwargs):
4647 return True
4748
4849
49- def cjdk_fetch_java (
50- vendor : str = "" , version : str = "" , raise_on_error : bool = True
51- ) -> None :
50+ def cjdk_fetch_java (vendor : str = "" , version : str = "" ) -> None :
5251 """Fetch java using cjdk and add it to the PATH."""
53- try :
54- import cjdk
55- except ImportError as e :
56- if raise_on_error is True :
57- raise ImportError (
58- "No JVM found. Please install `cjdk` to use the fetch_java feature."
59- ) from e
60- _logger .info ("JVM not found. Please install `cjdk` fetch java automatically." )
61- return
62-
6352 if not vendor :
6453 vendor = os .getenv ("JAVA_VENDOR" , _DEFAULT_JAVA_VENDOR )
6554 version = os .getenv ("JAVA_VERSION" , _DEFAULT_JAVA_VERSION )
@@ -70,20 +59,8 @@ def cjdk_fetch_java(
7059 os .environ ["JAVA_HOME" ] = str (home )
7160
7261
73- def cjdk_fetch_maven (url : str = "" , sha : str = "" , raise_on_error : bool = True ) -> None :
62+ def cjdk_fetch_maven (url : str = "" , sha : str = "" ) -> None :
7463 """Fetch Maven using cjdk and add it to the PATH."""
75- try :
76- import cjdk
77- except ImportError as e :
78- if raise_on_error is True :
79- raise ImportError (
80- "Please install `cjdk` to use the fetch_java feature."
81- ) from e
82- _logger .info (
83- "Maven not found. Please install `cjdk` fetch maven automatically."
84- )
85- return
86-
8764 # if url was passed as an argument, or env_var, use it with provided sha
8865 # otherwise, use default values for both
8966 if url := url or os .getenv ("MAVEN_URL" , "" ):
@@ -104,7 +81,7 @@ def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True)
10481 kwargs = {}
10582 if sha_len := len (sha ): # empty sha is fine... we just don't pass it
10683 sha_lengths = {40 : "sha1" , 64 : "sha256" , 128 : "sha512" }
107- if sha_len not in sha_lengths :
84+ if sha_len not in sha_lengths : # pragma: no cover
10885 raise ValueError (
10986 "MAVEN_SHA be a valid sha1, sha256, or sha512 hash."
11087 f"Got invalid SHA length: { sha_len } . "
@@ -114,8 +91,11 @@ def cjdk_fetch_maven(url: str = "", sha: str = "", raise_on_error: bool = True)
11491 maven_dir = cjdk .cache_package ("Maven" , url , ** kwargs )
11592 if maven_bin := next (maven_dir .rglob ("apache-maven-*/**/mvn" ), None ):
11693 _add_to_path (maven_bin .parent , front = True )
117- else :
118- raise RuntimeError ("Failed to find Maven executable in the downloaded package." )
94+ else : # pragma: no cover
95+ raise RuntimeError (
96+ "Failed to find Maven executable on system "
97+ "PATH, and download via cjdk failed."
98+ )
11999
120100
121101def _add_to_path (path : Union [Path , str ], front : bool = False ) -> None :
0 commit comments