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.

Maven installtions

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

Screen shot 2011 03 14 at 18 59 53

Feel free to comment for improvements.

Tagged on:         

5 thoughts on “Upgrade maven and switch between versions using bash on the mac

  • January 28, 2012 at 9:11 pm
    Permalink

    Hi Jettro,

    I haven’t tested it as is, but I think this should work:


    #!/bin/bash
    echo "Setting the maven implementation version"

    JAVA_SHARE=/usr/share/java
    MAVEN_LINK=/usr/share/maven

    # Get list of maven versions in ascending order
    VERSIONS=('' $(cd $JAVA_SHARE; find . -type d -name "apache-maven-[0-9\.]*" | cut -d '/' -f 2 | sort))

    # Choose the most recent maven2 and maven3 versions as the default
    for ((nr=1; nr < ${#VERSIONS[@]}; nr++)); do
    version=${VERSIONS[$nr]}
    case $version in
    apache-maven-2*) maven2=$version; continue;;
    apache-maven-3*) maven3=$version; continue;;
    esac
    done
    echo "Most recent Maven2 version: $maven2"
    echo "Most recent Maven3 version: $maven3"

    function choose_version() {
    # Print a menu with versions to choose from
    echo -e "\nPlease choose a version:"
    for ((nr=1; nr < ${#VERSIONS[@]}; nr++)); do
    version=${VERSIONS[$nr]}
    echo -e "$nr\t$version"
    done
    echo -e "X\tQUIT"

    # Keep asking for input until a valid choice is entered
    while true; do
    read choice
    case $choice in
    [0-9]*) if [ ! -z ${VERSIONS[$choice]} ]; then
    echo "Choice is $choice ${VERSIONS[$choice]}"
    maven=${VERSIONS[$choice]}
    return 0
    else
    echo "That option isn't on the menu."
    continue
    fi;;
    [xX]) echo "Bye"; exit 0;;
    *) echo "That option isn't on the menu."
    esac
    done

    # if this happens, something went wrong
    echo "Something went wrong. Exiting."
    exit 1
    }

    if [ -e "$MAVEN_LINK" ]
    then
    echo "Remove the maven soft link."
    sudo rm "$MAVEN_LINK"
    else
    echo "Maven soft link could not be found."
    fi

    maven=$maven3

    if [ $# == 0 ]
    then
    choose_version
    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

  • July 22, 2011 at 9:54 am
    Permalink

    Jetro,

    I followed the steps mentioned by you and it worked perfectly. Thanks.

  • March 16, 2011 at 1:56 am
    Permalink

    latest java update silently brought maven 3.0.2 but this script is still very useful to switch back to 2.* for builds using incompatibile plugins…

  • March 15, 2011 at 11:31 am
    Permalink

    I use a slightly different method on linux:

    Create two symlinks:

    sudo ln -s /usr/share/java/apache-maven-2.2.1 /usr/share/maven-2
    sduo ln -s /usr/share/java/apache-maven-3.0.3 /usr/share/maven-3

    To upgrade a minor version, just unpack and update the symlink. To switch between versions point your M2_HOME and PATH envs to the correct location:

    example script for switching to maven2:
    —————–
    $ cat setmvn2.sh
    #!/bin/bash

    OLD_M2_HOME=$M2_HOME
    M2_HOME=/usr/share/maven-2
    export M2_HOME

    PATH=${PATH/$OLD_M2_HOME/$M2_HOME}:
    export PATH M2_HOME

    mvn -v
    ———————

Comments are closed.