3

How can I make the maven installation permanent? After downloading the apache-maven to /usr/local, I need to print the series of "export" command lines every single time I want to use it. Even after I got the maven to work, maven doesn't work when I restart the terminal.

Added this to ~/.bash_profile:

export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
export PATH=$PATH:$M2_HOME/bin

Command-line:

$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.2.1/
$ export M2=$M2_HOME/bin
$ export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/home
$ export PATH=$M2:$JAVA_HOME/bin:$PATH
$ mvn –-version

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T12:37:52-05:00)
Maven home: /usr/local/apache-maven/apache-maven-3.2.1
Java version: 1.6.0_65, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.9.3", arch: "x86_64", family: "mac"

After restarting command-line:

$ mvn –-version
-bash: mvn: command not found

Instead of adding export PATH=$PATH:$M2_HOME/bin to ~/.bash_profile, I also tried adding the other export command-lines that sets M2, JAVA_HOME and PATH. This doesn't work.

wag0325
  • 1,008
  • 5
  • 18
  • 34

6 Answers6

5

After moving the application directory to /usr/local

Make a soft link in /usr/bin for universal access of mvn

sudo ln -s /usr/local/apache-maven-X.X.X/bin/mvn /usr/bin/mvn

Verify mvn installation

mvn --version
Thirumal
  • 8,280
  • 11
  • 53
  • 103
2

Since you are using Mac, I recommend that you install Homebrew (a package manager for OS X). Given that, you can install Maven using the brew command, e.g.

$ brew install maven
matsev
  • 32,104
  • 16
  • 121
  • 156
2

You need to make /etc/profile.d/maven.sh with the following content:

export M2_HOME=/opt/apache-maven-3.2.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH
gvlasov
  • 18,638
  • 21
  • 74
  • 110
1

Mac OSX I found two neat tutorials:

1) mkyong

2) journaldev

Win7 You need to set up environment variables. In Win 7, in Start menu you can type Edit the system environment variables, then click Environment variables. In System variables, add 2 New variables: M2_HOME=C:\Program Files\Apache Software Foundation\apache-maven-2.2.1 (this one is custom)

M2=%M2_HOME%\bin

After that, among the list of variables, find the one called Path, click Edit, and in the end, after this ; add %M2%; If you did everything right, and if you have JAVA_HOME set up, then Maven should be permanent.

gmode
  • 3,601
  • 4
  • 31
  • 39
0

Do you try putting it in a .profile file instead of .bash_profile?

See What's the difference between .bashrc, .bash_profile, and .environment?

.profile is handled at login, so you need to logout and login for changes to become active.

Community
  • 1
  • 1
GeertPt
  • 16,398
  • 2
  • 37
  • 61
0

In the Mac terminal, type below to open text editor

touch ~/.bash_profile
open ~/.bash_profile

Then in the editor, type:

export MAVEN_HOME=~/apache-maven-3.8.1
export PATH=$PATH:$MAVEN_HOME/bin

Close the editor and the contents will be saved. If you check the maven version, you will get response:

nisha$ mvn -version
Apache Maven 3.8.1 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d)
Maven home: /Users/nisha/apache-maven-3.8.1
Java version: 16.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "11.4", arch: "x86_64", family: "Mac"

Now, even if you close and open the terminal, you will be able to get the above output when you hit mvn -version

ngrashia
  • 9,869
  • 5
  • 43
  • 58