-
Notifications
You must be signed in to change notification settings - Fork 22
/
maven_setup.sh
38 lines (28 loc) · 1.53 KB
/
maven_setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/bin/bash
Author: Chris Parbey
log_file="/home/ubuntu/maven-log.txt"
current_datetime=$(date +"%Y-%m-%d %H:%M:%S")
# Check if the log file exists; if not, create it
if [ ! -e "$log_file" ]; then
touch "$log_file" || { echo "Error: Unable to create log file." >&2; exit 1; }
fi
# Set hostname
sudo hostnamectl set-hostname maven || { echo "Error: Unable to set hostname." | tee -a "$log_file"; exit 1; }
# Update and upgrade
sudo apt update -y || { echo "Error: Unable to update packages." | tee -a "$log_file"; exit 1; }
sudo apt upgrade -y || { echo "Error: Unable to upgrade packages." | tee -a "$log_file"; exit 1; }
# Change directory to /opt/ and run commands in a subshell
(
cd /opt/ || exit
# Install dependencies
sudo apt install default-jdk git -y || { echo "Error: Unable to install dependencies." | tee -a "$log_file"; exit 1; }
# Download and install Maven
sudo wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
sudo tar -xvzf apache-maven-3.9.6-bin.tar.gz
sudo rm apache-maven-3.9.6-bin.tar.gz
sudo mv apache-maven-3.9.6/ /opt/maven || { echo "Error: Unable to install Maven." | tee -a "$log_file"; exit 1; }
)
# Add Maven to the PATH in .bashrc of the ubuntu user (outside the subshell)
sudo -u ubuntu bash -c 'echo "export M2_HOME=/opt/maven" >> /home/ubuntu/.bashrc'
sudo -u ubuntu bash -c 'echo "export PATH=\$PATH:\$M2_HOME/bin" >> /home/ubuntu/.bashrc'
echo "Hostname changed, Java & Maven installed, Bashrc file updated at $current_datetime." | tee -a "$log_file"