środa, 5 września 2012

Ubuntu 12.04 - Installing both maven3 and maven2 as alternatives

Maven and /etc/alternatives


Ubuntu 12.04 comes with maven 3.0.4 as a default - that's great, but not for those who still needs maven 2.x. Unfortunately maven2 package can be installed can be installed only as the maven3 replacement - for those needed both, that sounds rather poor solution. Luckly there is /etc/alternatives mechanism that is perfect in such a case. It basically allows us having both with relativly easy way. But in that case alternatives does not work out of the box. The only way to handle this is installing maven2 binaries manually and register it as an alternative for mvn command.

How to do this - step by step solution: 

  1. Lets start with installing maven3 (3.0.4-2)
    $ sudo apt-get install maven
  2. Once done we look at recently installed mvn using update-alternatives command.
    $ update-alternatives --list mvn
     /usr/share/maven/bin/mvn
    $ update-alternatives --display mvn
    mvn - auto mode
      link currently points to /usr/share/maven/bin/mvn
    /usr/share/maven/bin/mvn - priority 150
      slave mvn.1.gz: /usr/share/maven/man/mvn.1.gz
      slave mvnDebug: /usr/share/maven/bin/mvnDebug
      slave mvnDebug.1.gz: /usr/share/maven/man/mvnDebug.1.gz
    Current 'best' version is '/usr/share/maven/bin/mvn'.
    Optionally fix the mvnDebug link into /usr/bin/mvnDebug
    ln -s /etc/alternatives/mvnDebug /usr/bin/mvnDebug
    
  3. Download maven 2 and extract it into /usr/share/maven2 folder.
  4. # cd /usr/share && wget http://archive.apache.org/dist/maven/binaries/apache-maven-2.2.1-bin.tar.gz
    
    # tar xzvf apache-maven-2.2.1-bin.tar.gz && mv apache-maven-2.2.1 maven2 && rm apache-maven-2.2.1-bin.tar.gz
    
  5. Add alternatives
    # update-alternatives --install /usr/bin/mvn mvn /usr/share/maven2/bin/mvn 200 --slave /usr/bin/mvnDebug mvnDebug /usr/share/maven2/bin/mvnDebug
    
    Priority 200 as a greater then 150 makes maven2 it the default (auto).
  6. Now you can display current alternatives for mvn command
    $ update-alternatives --display mvn
    mvn - auto mode
      link currently points to /usr/share/maven2/bin/mvn
    /usr/share/maven/bin/mvn - priority 150
      slave mvn.1.gz: /usr/share/maven/man/mvn.1.gz
      slave mvnDebug: /usr/share/maven/bin/mvnDebug
      slave mvnDebug.1.gz: /usr/share/maven/man/mvnDebug.1.gz
    /usr/share/maven2/bin/mvn - priority 200
      slave mvnDebug: /usr/share/maven2/bin/mvnDebug
    Current 'best' version is '/usr/share/maven2/bin/mvn'.
    
  7. In order to switch between maven versions use
     # update-alternatives --config mvn
    
  8. The same way you can add 3.2 release:
    # update-alternatives --install /usr/bin/mvn mvn /usr/share/maven32/bin/mvn 320 --slave /usr/bin/mvnDebug mvnDebug /usr/share/maven32/bin/mvnDebug
    

5 komentarzy:

  1. Thanks for your article, it saved me a lot of headache!

    OdpowiedzUsuń
  2. what about using netcarlson's ppa repo as an alternative also? https://launchpad.net/~natecarlson/+archive/maven3

    OdpowiedzUsuń
  3. Hey i am getting the following error on executing the following command mvn -version

    Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/classworlds/Launcher
    Caused by: java.lang.ClassNotFoundException: org.codehaus.classworlds.Launcher
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    Could not find the main class: org.codehaus.classworlds.Launcher. Program will exit.

    OdpowiedzUsuń
  4. Can you update the tutorial for Maven3 please - or a new post?!

    OdpowiedzUsuń