Skip to content
Snippets Groups Projects
Commit 6f92465e authored by michaelyoukeim's avatar michaelyoukeim
Browse files

Added maven to version mgmt

parent f041f9d8
No related branches found
No related tags found
No related merge requests found
import subprocess
import os
import subprocess
def list_java_versions():
"""Lists all available Java versions."""
try:
output = subprocess.check_output(
["update-java-alternatives", "--list"], text=True
["update-alternatives", "--list", "java"], text=True
)
print(output)
except subprocess.CalledProcessError as e:
......@@ -15,47 +14,58 @@ def list_java_versions():
def switch_java_version(java_version):
"""
Switches the Java version using update-java-alternatives and sets JAVA_HOME appropriately.
Switches the Java version using update-java-alternatives, sets JAVA_HOME, and configures Maven appropriately.
Args:
java_version (str): The name of the Java version to switch to, as listed by 'update-java-alternatives --list'.
"""
version_map = {
"1.6": "jdk1.6.0_45",
"1.7": "java-se-7u75-ri",
"1.8": "temurin-8-jdk-amd64",
"11": "temurin-11-jdk-amd64",
"21": "temurin-21-jdk-amd64"
}
if java_version == "21":
target_version = "temurin-21-jdk-amd64"
elif java_version == "1.8":
target_version = "temurin-8-jdk-amd64"
elif java_version == "11":
target_version = "temurin-11-jdk-amd64"
else:
maven_map = {
"1.6": "/opt/maven/apache-maven-3.0.5",
"1.7": "/opt/maven/apache-maven-3.6.3",
"1.8": "/opt/maven/apache-maven-3.6.3",
"11": "/opt/maven/apache-maven-3.6.3",
"21": "/opt/maven/apache-maven-3.6.3"
}
target_version = version_map.get(java_version)
if not target_version:
print(f"Invalid Java version specified: {java_version}")
return
try:
# Switch the Java version
subprocess.run(
["update-java-alternatives", "-s", target_version], check=True
["update-alternatives", "--set", "java", f"/usr/lib/jvm/{target_version}/bin/java"], check=True
)
print(f"Switched to Java version: {target_version}")
# Get the JAVA_HOME for the selected Java version
result = subprocess.run(
["update-alternatives", "--query", "java"],
capture_output=True,
text=True,
check=True,
)
# Extract the JAVA_HOME path
lines = result.stdout.split("\n")
for line in lines:
if "Value: " in line:
java_path = line.split("Value: ")[1].strip()
java_home = os.path.dirname(os.path.dirname(java_path))
os.environ["JAVA_HOME"] = (
java_home # Set JAVA_HOME in the current environment
)
# Set JAVA_HOME in the current environment and export it for all users
java_home = f"/usr/lib/jvm/{target_version}"
os.environ["JAVA_HOME"] = java_home # Set JAVA_HOME in the current environment
print(f"Set JAVA_HOME to {java_home}")
break
# Export JAVA_HOME for all shell sessions
with open('/etc/profile', 'a') as file:
file.write(f'\nexport JAVA_HOME={java_home}\n')
# Configure Maven
maven_home = maven_map.get(java_version)
os.environ["M2_HOME"] = maven_home
os.environ["PATH"] = f"{maven_home}/bin:" + os.environ.get("PATH", "")
print(f"Set M2_HOME to {maven_home}")
# Export M2_HOME for all shell sessions
with open('/etc/profile', 'a') as file:
file.write(f'\nexport M2_HOME={maven_home}\n')
file.write(f'\nexport PATH={maven_home}/bin:$PATH\n')
except subprocess.CalledProcessError as e:
print(f"Failed to switch Java version to {target_version}: {e}")
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment