Running maven2 and maven3 on the same machine

Maven is a great development process management tool. My personal experience started with version 2 of maven. I was using it for quite some time now and to be honest there is no need to get a new version because it just works.

However the world does not stand still and many people are on maven3 now. This brings some inconvenience since if you want to use a new shiny mvn dependency and it is on maven3 - your whole project needs to go one version up.

And there is no problem with that but it does mean that your .m2/repositoty is incompatible and what this means that if you have any manually installed artifacts there is going to be a hassle to reconfigure and re-installl everything.

So the solution is to somehow make both versions run along side each other, so that you can use either mvn or mvn3 to build your projects. However the anoying thing is that maven3 uses the same environment variables as maven 2 and therefore it just does not work OOTB.

Prerequisites: you have fully functional maven 2 installation and wish to "add" maven 3

What to do:

1. Install mvn3 (obviously) as recommended by your OS. (usually it means unzipping maven3 zip archive, however if you use installers like macports you may do things differently)

2. Specify M3_HOME environment variable and make sure your executable is on the PATH.

    On linux just add something like "export M3_HOME="/opt/local/share/java/maven3" to your profile, you also may want to add symbolik link to "ln -s $M3_HOME/bin/mvn mvn3" to directory of executables on your path

    On Windows use the environment variables tab on the system's pop up (and do not forget to update the path whilst you are there). Also you probably want to rename mvn.bat to mvn3.bat. Also in mvn.bat itself there is a self check for valid installation dir so you need to find a line that says something like: if exists "%M3_HOME%\bin\mvn.bat" goto init to if exists "%M3_HOME%\bin\mvn3.bat" goto init. Otherwise you may have error saying that the M3_HOME is invalid.

3. Specify alternative repository for maven3

    For this you need to go to $M3_HOME/conf and edit the setting.xml file. Find the localRepository tag and change it to: ${user.home}/.m3/repository

4. Change M2_HOME to M3_HOME in your mvn executable.

    HOME/bin/mvn (or mvn.bat) is just a script, so you can open it with a text editor of your choice and do a simple search and replace for M2_HOME to M3_HOME. So that when you launch mvn3 you will use M3_HOME and as for your maven 2 - everything will stay exactly as it was.

 

This should get you started.

If you use any other maven variables such as MAVEN_OPTS, you will need to create alternative variables for those too.

Let me know if there is anything extra missing from this guide.

 

 

This page was last updated on: 05/06/2014 10:11