Created
May 29, 2013 08:03
-
-
Save aheritier/5668699 to your computer and use it in GitHub Desktop.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
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
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
I use a bash function. Lets you have a different mvn for each terminal window