intruder
intruder

Reputation: 417

Unable to change path for Maven on Mac

I was trying to change M2_HOME in bash_profile to configure a new version of Maven. Earlier, it was set to 2.2.1. Now I'm trying to change the path to 3.3.3. This is my bash_profile

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home
export M2_HOME=/usr/local/apache-maven-3.3.3
#export M2_HOME=/usr/local/apache-maven-2.2.1
export PATH=$PATH:$JAVA_HOME/bin
export PATH=$PATH:$M2_HOME/bin
export CATALINA_HOME=/Library/Tomcat/apache-tomcat-7.0.68  

When I try to run source ~/.bash_profile and then mvn -version I get the following error -

$mvn -version
Error: Could not find or load main class org.codehaus.classworlds.Launcher  

Any suggestions to solve this please?

PS: I'm on OS X El Captain

Upvotes: 0

Views: 1697

Answers (3)

ILoveCode
ILoveCode

Reputation: 91

It is common, particularly in an environment where a product portfolio is quite large, to have to support multiple Maven versions. Similar to having to support multiple Java versions, you can create a script that will track and modify your environment accordingly. I use a Mac, so the notion of a jEnv type of mechanism is what I use for Java. On Windows, a similar concept can be used although It would take some coding to properly adjust the path settings.

Here's a /usr/local/bin/mvnEnv bash script that I use to quickly change my Maven runtime. It's not nearly as comprehensive as jEnv, but it works for me so perhaps it can work for you. Adjust the various parameters to conform to your various Maven installs and update your PATH appropriately, if on Windows. (I know you're using a Mac, so the Windows comment is for others that may have this issue on Windows.)

Just update your ~/.bash_profile to call this script with the appropriate parameters if you need a default. Then, when you need a different version of Maven, you can just execute the script like

mvnEnv v33

And voila, you've just quickly changed your Maven version! If you don't know what versions of Maven are supported, simply execute the mvnEnv command and a list of valid versions will be printed. You will, however, have to add any new versions of Maven to the script for the new version to be available.

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

    v22=/usr/local/Cellar/maven2/2.2.1/libexec/bin/mvn
    v30=/usr/local/Cellar/maven30/3.0.5/libexec/bin/mvn
    v31=/usr/local/Cellar/maven31/3.1.1/libexec/bin/mvn
    v32=/usr/local/Cellar/maven32/3.2.5/libexec/bin/mvn
    v33=/usr/local/Cellar/maven/3.3.9/libexec/bin/mvn

    if [ -e /usr/local/bin/mvn ] 
    then
        echo "Remove the maven soft link."
        sudo rm /usr/local/bin/mvn
    else
        echo "Maven soft link could not be found."
    fi

    maven=$v22

    if [ $# == 0 ] || [ -z "${!1// }" ] 
    then
        echo "No Arguments supplied, using default $maven"
        echo "Available versions:"
        echo "   v22 = 2.2.1"
        echo "   v30 = 3.0.5"
        echo "   v31 = 3.1.1"
        echo "   v32 = 3.2.5"
        echo "   v33 = 3.3.9"
    elif [ -e ${!1} ]
    then
        echo "Setting maven to use ${!1} via $1"
        maven=${!1}
    else
        echo "Using the default maven setting, provided argument [$1] is not recognized."
    fi

    echo "Creating new soft link to $maven";
    sudo ln -s $maven /usr/local/bin/mvn 

Upvotes: 0

Steve C
Steve C

Reputation: 19445

A simpler alternative is to set up some bash aliases. I have added the following to my ~/.bash_profile for switching between maven versions and Java versions:

export BASE_PATH=$PATH

#alias java5="export JAVA_HOME=`/usr/libexec/java_home -v1.5 -a x86_64 -d64`"
alias java6="export JAVA_HOME=`/usr/libexec/java_home -v1.6`"
alias java7="export JAVA_HOME=`/usr/libexec/java_home -v1.7`"
alias java8="export JAVA_HOME=`/usr/libexec/java_home -v1.8`"

# maven versions
alias m30="PATH=~/tools/apache-maven-3.0.5/bin:$BASE_PATH"
alias m31="PATH=~/tools/apache-maven-3.1.1/bin:$BASE_PATH"
alias m32="PATH=~/tools/apache-maven-3.2.5/bin:$BASE_PATH"
alias m33="PATH=~/tools/apache-maven-3.3.9/bin:$BASE_PATH"

Note the use of /usr/libexec/java_home for setting up JAVA_HOME which is similar to linux alternatives for switching java versions.

So, in a new terminal session the following:

[steve@steves-mbp ~]$ java8
[steve@steves-mbp ~]$ m33
[steve@steves-mbp ~]$ 

sets me up to use maven 3.3 and Java 8.

Please also take into account the comment by ~khmarbaise regarding M2_HOME and forget that this environment variable exists.

Upvotes: 2

dmitchell
dmitchell

Reputation: 43

Add a new symlink for mvn3 worked for me

ln -s /usr/local/apache-maven-3.3.3/bin/mvn /usr/local/User/bin/mvn3

Upvotes: 0

Related Questions