Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
DSSE_Group1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Neha Pokharel
DSSE_Group1
Commits
6f92465e
There was an error fetching the commit references. Please try again later.
Commit
6f92465e
authored
May 14, 2024
by
michaelyoukeim
Browse files
Options
Downloads
Patches
Plain Diff
Added maven to version mgmt
parent
f041f9d8
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/hadoop_analysis/java_version_manager.py
+42
-32
42 additions, 32 deletions
src/hadoop_analysis/java_version_manager.py
with
42 additions
and
32 deletions
src/hadoop_analysis/java_version_manager.py
+
42
−
32
View file @
6f92465e
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
'
\n
export 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
'
\n
export M2_HOME=
{
maven_home
}
\n
'
)
file
.
write
(
f
'
\n
export 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment