Jar Bar Guide to install Maven 3 on Linux by Yucca Nel
At The jar Bar I have written a Bash script that automates the Maven install process for not only Ubuntu, but other Linux systems too. As a Java developer, Maven is at the forefront when it comes to tools to use. Maven takes the hassle out of locating, downloading and managing common frameworks like Hibernate and servers like Tomcat for use in your projects as a Java developer.
Download the Maven install script
The script below will download Maven and install it. Also links the content of the Maven bin to your '/usr/bin' location so that you can use Maven right away.
Maven for the VirtualBox Developer
You can share your Maven install from your host to guest our system(s). Simply set up shared folders for your existing Maven and your '.m2' directory under the names of maven and m2. My script will create additional scripts in your '/usr/local/bin' directory that allow you to mount and unmount Maven as needed.
#!/bin/bash
#Author: Yucca Nel http://thejarbar.org
#Modify these variables as needed...
tempWork=/tmp/work
defaultStartScript=/etc/init.d/rc.local
defaultMaven=3.0.4
locBin=/usr/local/bin
mavenUsrLib=/usr/lib/maven
sudo mkdir -p $mavenUsrLib
mkdir -p $HOME/.m2
read -p "Please [Enter] full path name of your local startup script ($defaultStartScript is the default). Please
make sure on this before providing a value by consulting documentation for your system:" locStartScript
locStartScript=${locStartScript:-$defaultStartScript}
read -p "Please [Enter] Maven Version ($defaultMaven is default):" mavenVersion
mavenVersion=${mavenVersion:-$defaultMaven}
if [ ! -f $locStartScript ]
then
echo "The file you provided could not be found. Remember to include the full path and try again. Exiting in 7 secs..."
sleep 7
exit 1
fi
mkdir -p $tempWork
cd $tempWork
wget http://mirrors.powertech.no/www.apache.org/dist//maven/binaries/apache-maven-$mavenVersion-bin.tar.gz
tar -zxvf $tempWork/*
#Move it to a more logical location
sudo mv -f $tempWork/apache-maven-$mavenVersion $mavenUsrLib/
sudo ln -f -s $mavenUsrLib/apache-maven-$mavenVersion/bin/* /usr/bin/
#If you have Maven on Windows and use VirtualBox, you can set up the maven to be a virtualbox shared folder.
#The name must match the name used below (ignore if irrelevant to you).
if [ -f /sbin/mount.vboxsf ]
then
sudo /sbin/umount $HOME/.m2
sudo /sbin/umount $mavenUsrLib
sudo /sbin/mount.vboxsf .m2 $HOME/.m2
sudo /sbin/mount.vboxsf maven $mavenUsrLib
fi
if mountpoint -q $HOME/.m2 && mountpoint -q $mavenUsrLib
then
tar -zxvf $tempWork/*
sudo mv -f $tempWork/apache-maven-$mavenVersion $mavenUsrLib/
#Add it to the start script to automate process...
if ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locStartScript
then
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2", sudo tee -a $locStartScript
fi
if ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locStartScript
then
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib", sudo tee -a $locStartScript
fi
echo "exit 0", sudo tee -a $locStartScript
sudo chmod +x $locStartScript
#Create a mount and unmount script file...
rm -rf $tempWork/*
echo '#!/bin/bash' > $tempWork/maven-mount.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" >> $tempWork/maven-mount.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib" >> $tempWork/maven-mount.sh
echo "echo 'mounted maven'" >> $tempWork/maven-mount.sh
echo "exit 0" >> $tempWork/maven-mount.sh
echo '#!/bin/bash' > $tempWork/maven-umount.sh
echo "sudo umount $HOME/.m2" >> $tempWork/netbeans-umount.sh
echo "sudo umount $mavenUsrLib" >> $tempWork/netbeans-umount.sh
echo "echo 'unmounted maven'" >> $tempWork/maven-mount.sh
echo 'exit 0' >> $tempWork/maven-umount.sh
#Script for mounting ALL VirtualBox shared solders....
#If there isn't one create one...
if [ ! -f $locBin/mount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2", sudo tee -a $tempWork/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib", sudo tee -a $tempWork/mount-all-from-host.sh
echo "exit 0", sudo tee -a $tempWork/mount-all-from-host.sh
#Otherwise if there is one, but no mount, add one...
elif ! grep "sudo /sbin/mount.vboxsf .m2 $HOME/.m2" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf .m2 $HOME/.m2", sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0", sudo tee -a $locBin/mount-all-from-host.sh
elif ! grep "sudo /sbin/mount.vboxsf maven $mavenUsrLib" $locBin/mount-all-from-host.sh
then
sudo sed -ie '$d' $locBin/mount-all-from-host.sh
echo "sudo /sbin/mount.vboxsf maven $mavenUsrLib", sudo tee -a $locBin/mount-all-from-host.sh
echo "exit 0", sudo tee -a $locBin/mount-all-from-host.sh
fi
#Script for unmounting ALL VirtualBox shared folders...
#If there isn't one create one...
if [ ! -f $locBin/umount-all-from-host.sh ]
then
echo '#!/bin/bash' > $tempWork/umount-all-from-host.sh
echo "sudo umount -a -t vboxsf", sudo tee -a $tempWork/umount-all-from-host.sh
echo "echo 'unmounted all VirtualBox shared folders'", sudo tee -a $tempWork/umount-all-from-host.sh
echo "exit 0", sudo tee -a $tempWork/umount-all-from-host.sh
fi
sudo chmod +x $tempWork/*
sudo mv -f $tempWork/*.sh $locBin/
rm -rf $tempWork
fi
sudo rm -rf $tempWork
sudo /sbin/reboot
exit 0