Over three years a go I wrote a blog post explaining the steps to upgrade you maven installation on Mac OS X Leopard. I have been using snow leopard now for a while and things changed a bit. In one of the comments to that post Oskar Carlstedt mentioned the steps required on Snow Leopard. This is all fine if you upgrade a minor version, but with maven 3 things changed a lot. I have some projects that require maven 2, we did not have time yet to fix problems we have with maven 3. Still I prefer using maven 3. Therefore I have to change between versions often. I decided to create a bash script to help me out.
Within this blogpost I’ll explain the installation of maven on Snow Leopard and I’ll present the bash script that can help you to switch between installations of maven.
Maven on the Mac (Snow Leopard)
By default maven is installed in the path: /usr/share/java. The maven executables are installed as maven-3.0.2 and maven-2.2.1. Since the upgrade path is a bit slow I tend to install my own maven binaries. I extract them as provided by apache using the apache-maven-x.x.x format.
A soft link is available in /usr/share/maven. This is the link that points to one of the maven binaries. This is the place where you switch between versions of maven. You can remove the soft link and create one to the right maven home folder. A small catch is that you need to be root to remove and create the link. Using sudo is good enough.
The commands to do this are:
cd /usr/share sudo rm maven ln -s /usr/share/java/apache-maven-3.0.2 maven
So not a lot of work, but just annoying to do one or more times a day. Therefore I have wanted to have a bash script to do it for me.
Introducing bash
When automating something on the Mac I tend to use bash. I am not an expert, but using google I can do interesting things. I’ll step to the script. To start off, define the two maven installations that I currently use:
maven2=apache-maven-2.2.1 maven3=apache-maven-3.0.2
Next step is to remove the current soft link if it exists.
if [ -e /usr/share/maven ] then sudo rm /usr/share/maven fi
Next is to store the maven implementation version in a variable
maven=$maven3 if [ $# == 0 ] then echo "No Arguments supplied, using default maven 3" elif [ $1 == 2 ] then maven=$maven2 else echo "Using the default maven setting, provided argument [$1] is not recognized" fi
The default is maven 3, than we check if arguments are provided to the script. If no arguments are provided, we do nothing and use the default. if an argument is provided we check if it is 2. If so, we set maven 2 as the implementation. If something else than 2 is provided we take the default which is maven 3. The final step is to create the soft link again.
sudo ln -s /usr/share/java/$maven /usr/share/maven
Using spaces
One thing that took me a few moments to understand is the use of spaces within bash. maven=$maven2 is not the same as maven = $maven2
The complete script
#!/bin/bash echo "Setting the maven implementation version" maven2=apache-maven-2.2.1 maven3=apache-maven-3.0.2 if [ -e /usr/share/maven ] then echo "Remove the maven soft link." sudo rm /usr/share/maven else echo "Maven soft link could not be found." fi maven=$maven3 if [ $# == 0 ] then echo "No Arguments supplied, using default maven 3" elif [ $1 == 2 ] then echo "Setting maven to use to maven 2" maven=$maven2 else echo "Using the default maven setting, provided argument [$1] is not recognized" fi echo "Creating new soft link to $maven" sudo ln -s /usr/share/java/$maven /usr/share/maven
Room for improvement
It would be nice to do a scan of the directory /usr/share/java and provide you with options to select from. You could also select the most recent version of maven 2 or 3 or even 2.2 and 3.0. If I have a need for that I’ll update the script 🙂
Concluding
That is it, you now have a script that you can call with the implementation you want to set. I called mine setmaven.sh and you would call it like:
./setmaven.sh 2
You can check if it is working with the well known mvn –version command
Feel free to comment for improvements.