Set java home to maven

MacOS Set Java Home, Maven Home, M2, Maven Opts Example

On macOS set java_home, maven_home is not as easy as in windows after you install JDK or maven. Do not worry, this article will tell you how to do it in macOS.

1. Set JAVA_HOME ( Mac ).

  1. First, all environment variable settings are saved in the current user’s .bash_profile file, this file is saved in the current user’s home directory. Run cd ~ in terminal to go to the current user home directory.
  2. Then execute ls -l .bash_profile to make sure the file exists. You can also run ls -al to list all files under your home directory.
  3. Execute vim .bash_profile command in terminal and add below export command in it. In vim, press the esc key first then press i key to insert text, click esc then :wq to save and quit.
export JAVA_HOME="$(/usr/libexec/java_home)"
192:~ zhaosong$ $(/usr/libexec/java_home) -bash: /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home: is a directory
export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin

2. Set MAVEN_HOME ( Mac ).

Set MAVEN_HOME on mac is similar to set JAVA_HOME on mac, but you need to install maven first.

  1. Go to https://maven.apache.org/download.cgi to download the latest maven archive file.
  2. Unzip the zip file to a local folder ( for example /Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4 ). In vim click esc key on the keyboard then press i key to insert the below export command in .bash_profile.
export MAVEN_HOME="/Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4"
export JAVA_HOME="$(/usr/libexec/java_home)" export M2_HOME="/Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4" export MAVEN_HOME=$M2_HOME export M2=$M2_HOME/bin export MAVEN_OPTS="-Xms256m -Xmx512m"
192:~ zhaosong$ mvn -v -bash: mvn: command not found
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:$:/Users/zhaosong/Documents/WorkSpace/tool/spring-2.0.2.RELEASE/bin:$M2_HOME/bin" export PATH
192:~ zhaosong$ mvn -v The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE
192:~ zhaosong$ java -version java version "1.8.0_172" Java(TM) SE Runtime Environment (build 1.8.0_172-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.172-b11, mixed mode) 192:~ zhaosong$ mvn -v Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00) Maven home: /Users/zhaosong/Documents/WorkSpace/tool/apache-maven-3.5.4 Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_172.jdk/Contents/Home/jre Default locale: en_CN, platform encoding: UTF-8 OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"

3. Run Maven With Multiple JDK Version.

  1. When you run the maven command to build a java project on mac, it gets the JDK version from mac JAVA_HOME variable.
  2. But if there are multiple JDK versions installed on your machine, and you want to use different JDK version to build different java project, You can use the maven compiler plugin to let each maven project use its own JDK.
  3. Edit your maven project pom.xml file, and add the below XML in it.
   org.apache.maven.plugins maven-compiler-plugin 3.6.2 1.8 1.8     or 1.8 1.8    .   
export JAVA_HOME=`/usr/libexec/java_home -v 1.9.0`
# From below output we can see maven use JDK 1.9 which is configured in ~/.mavenrc file. $ ./mvn -version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /Users/songzhao/Downloads/apache-maven-3.6.3 Java version: 1.9., vendor: AdoptOpenJDK, runtime: /Library/Java/JavaVirtualMachines/adoptopenjdk-9.jdk/Contents/Home/jre Default locale: en_US, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.7", arch: "x86_64", family: "mac" # Below is mac OS used JDK version. $ java -version openjdk version "1.8.0_212" OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_212-b03) OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.212-b03, mixed mode)

Источник

How to Install Maven on Windows 10

In this quick tutorial, we are going to highlight how to install Maven on Windows 10.

In short, Maven is a build automation and dependency management tool for Java based applications. To build a project, Maven uses its project object model (POM) and a set of plugins.

For this tutorial, I will be using:

Note: We have also written a nice article on how to install maven on mac. Feel free to check it.

Install JDK and Set up JAVA_HOME

The installation process of Apache Maven is quite simple, we just need to extract the Maven’s zip file and set up some environment variables.

However, Maven requires JDK to perform some important operations such as code compilation.

So, before installing Maven on Windows 10, we need to make sure that Java JDK is installed and the JAVA_HOME environment variable is properly configured on our machine.

Please refer to how to install on Windows 10 for more details.

Another important thing to keep in mind is that:

  • Apache Maven 3.3.1+ requires JDK 1.7 or above
  • Apache Maven 3.2.1->3.2.5 requires JDK 1.6
  • Apache Maven 2.2 to 3.1.1 requires JDK 1.5
  • Apache Maven 2.0 to 2.1 requires Java 1.4

We can type the following commands to check which JDK version is installed on our machine:

 C:\Users\Asus>echo %JAVA_HOME% C:\Program Files\Java\jdk1.8.0_261 C:\Users\Asus>java -version java version "1.8.0_261" Java(TM) SE Runtime Environment (build 1.8.0_261-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode) C:\Users\Asus> 

As we can see, the installed version of JDK is 1.8.0_261.

Download Apache Maven for Windows

We can download Maven directly from the official website: https://maven.apache.org/download.cgi. Make sure to download the latest stable release which is now 3.8.6.

Download Apache Maven

Install Apache Maven on Windows 10

Now, let’s download apache-maven-3.8.6-bin.zip and unzip it to a specific folder of our choice, for example: C:\Users\Asus\soft\apache-maven-3.8.6

Unzip Apache Maven Zip File

That’s all, NO installation is required to work with Mvn. Cool, right?

Now, let’s briefly explain what contains each directory:

  • bin contains all the batch and shell scripts that are necessary for Maven execution
  • boot contains all the tools required to start Maven
  • conf contains the basic configuration such as the default settings.xml file
  • lib, as the name implies, contains all the libraries and extensions used by Maven

Add MAVEN_HOME Environment Variable

The next step is to configure the MAVEN_HOME variable on Windows 10.

Since Maven 3.5.0, setting up MAVEN_HOME environment variable is optional. However, it is a good practice since many IDEs and tools use it to run Maven.

MAVEN_HOME should point to the folder where we extracted the Maven zip file, which is C:\Users\Asus\soft\apache-maven-3.8.6 in our case.

Follow these steps to add the MAVEN_HOME environment variable:

1- Type “edit” in the Window search box, then click on the “Edit the system environment variables”

Edit environment variable

2- Next, select the “Environment Variables…” button

3- Click on the “New…” button and put MAVEN_HOME as variable name and C:\Users\Asus\soft\apache-maven-3.8.6 as variable value

add MAVEN_HOME environment variable

Another way to set up MAVEN_HOME is to use command lines. To do so, open a new command prompt as administrator and type the following command:

 C:\Windows\system32>setx /M MAVEN_HOME "C:\Users\Asus\soft\apache-maven-3.8.6" SUCCESS: Specified value was saved. 

Add %MAVEN_HOME%\bin to the PATH

Now let’s add the last missing piece of the puzzle. This step is very important to run the Mvn command everywhere directly from the command prompt.

To do so conveniently, we need to edit the PATH variable by appending the Maven bin folder %MAVEN_HOME%\bin.

Add bin folder to Path

Once done, we can save the applied changes by clicking on the OK button.

Please bear in mind that we can accomplish the same thing using this command line:

 C:\Windows\system32>setx /M PATH "%MAVEN_HOME%\bin;%PATH%" 

Verify Mvn Installation

Now that we put all the pieces together, let’s test if Apache Maven is successfully installed on Windows 10.

To verify the installation, we run:

 C:\Users\Asus>mvn --version Apache Maven 3.8.6 (05c21c65bdfed0f71a2f2ada8b84da59348c4c5d) Maven home: C:\Users\Asus\soft\apache-maven-3.8.6\bin\.. Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: C:\Program Files\Java\jdk1.8.0_261\jre Default locale: en_US, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows" C:\Users\Asus>echo %MAVEN_HOME% C:\Users\Asus\soft\apache-maven-3.8.6 C:\Users\Asus> 

The mvn command is a direct shortcut to the mvn.bat batch file. This is the main idea behind adding the bin folder to the PATH environment variable.

As shown above, the command indeed displays the Maven version, Maven bin folder, Java version, and operating system information.

Common Issues

Here, we are going to put the focus on the common problems that we may face while installing Maven.

1) mvn is not recognized as an internal or external command

 C:\Users\Asus>mvn --version 'mvn' is not recognized as an internal or external command, operable program or batch file. 

This means that the installation is not done properly, make sure %MAVEN_HOME%\bin is prepended to the PATH variable the right way.

2) JAVA_HOME environment variable is not defined correctly

 C:\Users\Asus>mvn --version The JAVA_HOME environment variable is not defined correctly This environment variable is needed to run this program NB: JAVA_HOME should point to a JDK not a JRE 

This simply means that JDK is not installed and the JAVA_HOME variable is not properly configured.

3) Error: Could not find or load main class org.codehaus.plexus.classworlds.launcher.Launcher

In this case, you need to download the binary zip archive file, instead of the source zip archive.

Conclusion

In this short article, we walked you through the steps of how to install Maven on Windows 10.

Please feel free to reach out to me if you encounter any issues.

Liked the Article? Share it on Social media!

If you enjoy reading my articles, buy me a coffee ☕. I would be very grateful if you could consider my request ✌️

Источник

Create Maven Projects with Java 11

Note: After updating the JAVA_HOME environment variable, you need to restart your IDE.

Configuring Maven Project pom.xml

By default your version of Maven might use an old version of the maven-compiler-plugin that is not compatible with Java 9 or later versions. To target Java 9 or later, you should at least use version 3.6.0 of the maven-compiler-plugin and set the maven.compiler.release property to the Java release you are targetting (e.g. 9, 10, 11, 12, etc.). [1]

properties>
maven.compiler.release>11 maven.compiler.release>
properties>

build>
pluginManagement>
plugins>
plugin>
groupId>org.apache.maven.plugins groupId>
artifactId>maven-compiler-plugin artifactId>
version>3.8.1 version>
plugin>
plugins>
pluginManagement>
build>
plugin>
groupId>org.apache.maven.plugins groupId>
artifactId>maven-compiler-plugin artifactId>
version>3.8.1 version>
configuration>
release>11 release>
configuration>
plugin>

Configuring IDE

Configuring Java 11 for IntelliJ IDEA

  1. Open Project Settings (Ctrl + Shift + Alt + S) –> Project –> Project SDK: 11, Project language level: 11.
  2. Open Settings (Ctrl + Alt + S) –> Build, Execution, Deployment –> Compiler –> Java Compiler –> Project bytecode version: 11

Источник

Читайте также:  Controller php namespace missing
Оцените статью