Is there a robust way to set MAVEN_HOME in my ~/.zshrc without specifying the version number? (MacOS)

2.7k views Asked by At

Context: I install maven with brew install maven

In my ~/.zshrc I have:

# Maven (mvn --version)
export MAVEN_HOME="$HOME/usr/local/Cellar/maven/3.6.3"
export PATH=$MAVEN_HOME/bin:$PATH

Is there a way to set my MAVEN_HOME in a way that allows me to not have to increment it when Maven is upgraded to 3.6.4?

It is somewhat tedious to have to go into my config file and update it every time I update Maven! :)

This would also help an onboarding/setup script that could download maven and programmatically set up a ~/.zshrc or ~/.bashrc config

3

There are 3 answers

0
Mark Han On BEST ANSWER

Per @chepner: /usr/local/bin/mvn is a symlink to the latest installed version when installed via homebrew.

I changed export MAVEN_HOME="/usr/local/Cellar/maven/3.6.3/mvn" to export MAVEN_HOME="/usr/local/bin/mvn" in my ~/.zshrc config and everything is good to go.

2
Rando Shtishi On

What i have done to solve this problem is to create a symbolic link that points to maven installation version.

ln -s maven_3.6.4_path maven_slink

And set the MAVEN_HOME=maven_slink in configuration file. And every time i need to change maven version all i have to do is drop maven_slink and create a new maven_slink that points to the maven installation. I don't need to go change configuration files every time I install a new version of maven.

1
AudioBubble On

I work on windows and i have created folder dev-env and the path of this folder i have added in PATH variable.

I have installed the version of java jdk8, jdk9, jdk11, jdk13. I have created bat files and place them in dev-env : jdk8.bat ,jdk9.bat, jdk9.bat, jdk11.bat and jdk13.bat. Inside a bat files I do the switch of version of java.

@echo off 
set JAVA_HOME=path to jdk version
set PATH = %JAVA_HOME%\bin+PATH
java -version

Same thing in maven . I created different bat files maven3.bat, maven36.bat.

@echo off 
set MAVAEN_HOME=path to jdk version
set PATH = %MAVEN_HOME%\bin+PATH
mvn -version

So , If i want to switch from one version to another. I open the cmd and the run the bat file that has the version that i want.