'Install maven to any linux system using bash script

Is there any possible way I can install maven on any of the linux machine using a bash script ?



Solution 1:[1]

According to Mkyong, you can install Maven using sudo:

sudo apt-get install maven

then, run

mvn -version

to check that it installed.

Solution 2:[2]

Yes you can install mvn with bash script, example below

mvn_version=${mvn_version:-3.8.5}
url="http://www.mirrorservice.org/sites/ftp.apache.org/maven/maven-3/${mvn_version}/binaries/apache-maven-${mvn_version}-bin.tar.gz"
install_dir="/opt/maven"

mkdir ${install_dir}
curl -fsSL ${url} | tar zx --strip-components=1 -C ${install_dir}
cat << EOF > /etc/profile.d/maven.sh
#!/bin/sh
export MAVEN_HOME=${install_dir}
export M2_HOME=${install_dir}
export M2=${install_dir}/bin
export PATH=${install_dir}/bin:$PATH
EOF
source /etc/profile.d/maven.sh
echo maven installed to ${install_dir}
mvn --version

you need to add source /etc/profile.d/maven.sh to ~/.bash_profile or basrch

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 baranskistad
Solution 2