Here is the documentation on Ubuntu Environment variables.
What we want to do here is persist our JAVA_HOME path.
Most commonly, our java installations go in /usr/lib/jvm. To list the installations present,
ls /usr/lib/jvm
I have /usr/lib/jvm/java-8-openjdk-amd64 among them and that is installation I want to use for maven.
We can add our entry for JAVA_HOME entry to ~/.pam_environment and make it DEFAULT or OVERRIDE - that's your call.
Use whatever text editor you want to open the file (I am using a text editor that is lying around in my system - it's name is xed. You may use gedit or vim or anything)
xed ~/.pam_environment
You will find things like:

Now, let us add a new entry for our JAVA_HOME environment variable. In a new line, enter:
JAVA_HOME DEFAULT=<your path to your java home location>
or
JAVA_HOME DEFAULT=${JAVA_HOME} OVERRIDE=<path to your java home location>
As my java home location is '/usr/lib/jvm/java-8-openjdk-amd64', I am entering
JAVA_HOME DEFAULT=${JAVA_HOME} OVERRIDE=/usr/lib/jvm/java-8-openjdk-amd64
Now, log out of your system and log back in for the change to take
effect.
Let's check the maven version now.
mvn -version
and it should work :

update-java-alternatives
to update both compiler and runtime – Benjamin Maurer Feb 05 '15 at 18:48