Maven set up required everytime i close the terminal

Viewed 364

i'm very new to this world.. so forgive me for silly explanations.

I've installed maven and all the setting is done. Also i created a maven.sh

Content:

 # Apache Maven Environment Variables        
 # MAVEN_HOME for Maven 1 - M2_HOME for Maven 2        
 export JAVA_HOME=/usr/lib/jvm/java-11        
 export M2_HOME=/opt/apache-maven        
 export MAVEN_HOME=/opt/apache-maven        
 export PATH=${M2_HOME}/bin:${PATH}        

At this point everything is working as expected, but every time i close the terminal it seems to forget some settings.. I have to open the terminal and execute the following commands to make it work again:

 ~$ cd /etc/profile.d
 ~$ source maven.sh

I don't know if it is just the way to use maven or i'm missing something, but it's kind of annoying writting the same commands every single time i want to execute maven.. so i would appreciate if you can explain me a way to automate it.

Some extra information:

OS : Ubuntu 20.0.4

JDK : 11

Not first time creating JAVA_HOME variable

After executing those commands above, it shows the correct JAVA_HOME, but it's forgotten after closing the terminal and shows the wrong JAVA_HOME again

Let me tell you guys, this is my first question on stackoverflow, so i will understand if you want to correct me about my manners and my bad english (it's not my mother tongue, i have to get used to it)

Thank you in advance!

3 Answers

I can think of a couple of ways you could resolve this problem given your situation, which is not maven-specific but rather terminal-specific --

  1. Add a reference to your maven.sh script to your profile so that it runs when you start a terminal session. This can typically be accomplished by creating (or adding to) the .bash_profile file in your home directory. You could either add the contents of your maven.sh script to that file, or add source /etc/profile.d/maven.sh to it. From that point forward when you start a terminal session, the script gets run automatically and your variables are set correctly. See https://joshstaiger.org/archives/2005/07/bash_profile_vs.html or the bash man page for some more details about the login shell
  2. If you've already done the first step and it didn't work, consider checking to see if those environment variables are being set in another place (check ~/.bashrc, ~/.bash_profile, and anywhere your particular shell distribution / OS checks for startup scripts
  3. Lastly, I recommend using SDKMan! to manage maven installs without these headaches -- but your mileage may vary. It handles keeping track of the environment variables, java version, &c and allows multiple Java/Maven versions to be installed and managed. https://sdkman.io

Sources:

You can add these properties to individual user settings file. This file will be available in home directory

vi $HOME/.bash_profile

So everytime, you login to terminal, all these settings will be executed.

I solved the problem by doing a logout and login in Ubuntu

Related