Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to switch between various Maven versions (One day perhaps we may have a renv, jenv, like ...) Works on MacOs Put your Maven installations under $MVN_VERSIONS_DIRECTORY
#!/bin/bash
MVN_VERSIONS_DIRECTORY="/Users/arnaud/Applications/"
MVN_DIRECTORY_TEMPLATE="apache-maven-"
versions=`ls -1 ${MVN_VERSIONS_DIRECTORY} | grep ${MVN_DIRECTORY_TEMPLATE}'[2-9].[0-9]' | sed 's/^.*maven-//g'`
echo -e "\033[1;32mAvailable Versions:\033[0m " $versions
if [ "$1" == "" ]; then
exit
fi
echo -e "\033[1;32mCurrent Version:\033[0m `mvn -v | head -1`"
pushd ${MVN_VERSIONS_DIRECTORY} > /dev/null 2>&1
VERFOUND=`ls | grep $1 | head -n 1`
if [ "$VERFOUND" != "${MVN_DIRECTORY_TEMPLATE}$1" ]; then
BASE="`basename $0`"
echo Error: Could not change Maven -- version $1 not installed!
echo Run $BASE without arguments to see a list of installed versions.
exit 127
fi
# If we get here the user asked for a valid version, so configure it
rm -f ${MVN_VERSIONS_DIRECTORY}apache-maven
ln -fhs ${MVN_VERSIONS_DIRECTORY}${MVN_DIRECTORY_TEMPLATE}$1 ${MVN_VERSIONS_DIRECTORY}apache-maven
_system_mvn_target=`ls -l /usr/bin/mvn | awk '{print $11}'`
if [ "$_system_mvn_target" != "${MVN_VERSIONS_DIRECTORY}apache-maven/bin/mvn" ]; then
echo You must now enter your Mac OS X password to change Maven.
sudo ln -fhsv ${MVN_VERSIONS_DIRECTORY}apache-maven/bin/mvn /usr/bin/mvn
fi
popd > /dev/null 2>&1
echo -e "\033[1;32mNew Version:\033[0m `mvn -v | head -1`"
# To activate if you have growl and growlnotify script installed
#growlnotify -n Maven -m "Maven now in $1"
@stephenc

This comment has been minimized.

Copy link

@stephenc stephenc commented May 29, 2013

I use a bash function. Lets you have a different mvn for each terminal window

usemvn () 
{ 
    if [ -z "$1" -o ! -x "/usr/share/java/maven-$1/bin/mvn" ]; then
        local prefix="Syntax: usemvn ";
        for i in /usr/share/java/maven-*;
        do
            if [ -x "$i/bin/mvn" ]; then
                echo -n "$prefix$(basename $i | sed 's/^maven-//')";
                prefix=" | ";
            fi;
        done;
        echo "";
    else
        if [ -z "$MAVEN_HOME" ]; then
            export PATH=/usr/share/java/maven-$1/bin:$PATH;
        else
            export PATH=$(echo $PATH|sed -e "s:$MAVEN_HOME/bin:/usr/share/java/maven-$1/bin:g");
        fi;
        export MAVEN_HOME=/usr/share/java/maven-$1;
    fi
}
@fdrouet

This comment has been minimized.

Copy link

@fdrouet fdrouet commented May 29, 2013

I also add the following code snippet to have the bash completion on your usemvn function :

# Add tab completion for usemvn command
complete -o "default" -o "nospace" -W "$(for i in /usr/share/java/maven-*; do if test -x "$i/bin/mvn"; then echo "$(basename $i | sed 's/^maven-//')"; fi; done)" setMaven
@stephenc

This comment has been minimized.

Copy link

@stephenc stephenc commented May 29, 2013

You mean

# Add tab completion for usemvn command
complete -o "default" -o "nospace" -W "$(for i in /usr/share/java/maven-*; do if test -x "$i/bin/mvn"; then echo "$(basename $i | sed 's/^maven-//')"; fi; done)" usemvn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment