How to setup maven environment

Posted on

Step 1 : Download maven archives

First we need to download the maven archives from maven repository using this link At the time of creating this tutorial, the latest version of Maven was 3.9.4.

The name of the archive file is

Copy this file to a directory of your choice. Let's say

c://maven for windows
/usr/local/maven on mac

Step 2 : Set maven environment variable and path

We need to set following environment variables:-

  • M2_HOME
  • M2
For Windows:-

  • Create a new System Variable and add the variable name as M2_HOME and variable value as c://maven.
  • Create a new System Variable and add the variable name as M2 and variable value as M2=%M2_HOME%/bin.
  • Create a new System Variable and add the variable name as MAVEN_OPTS and variable value as -Xms256m -Xmx512m.
    For Mac:-

    Open terminal and enter the following commands:
    • export M2_HOME=/usr/local/maven
    • export M2=$M2_HOME/bin
    • export MAVEN_OPTS=-Xms256m -Xmx512m
      Now we need to add Maven bin directory to system path.

      For Windows:-

      • Append the %M2% to the end of the system path variable.

      For Mac:-

      Open the terminal and run the following command:
      • export PATH=$PATH:$M2

      Step 3 : Verify maven installation

      Now we need to verify the maven installation. To verify the maven installation run the following command. mvn --version

      If you see the maven version number which you have downloaded, then you have successfully configured maven in your system.

      Step 4 : Create Simple Maven Project

      Now lets create a maven project by running the following command. mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
      While running the command it may ask you following information. Type as the following:

      • groupId : com.example.simpletest
      • artifactId : simple-test
      • version : 1.0.0
      • package : com.example.simpletest
      Once the project is created, change directory to the project directory and run the mvn test command.

      cd simple-test
      mvn test

      Step 5 : Run tests

      If you are able to see the following at the end of the console log, You have successfully configured maven.

      [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

      Note: Sometimes you see Compilation failure. To fix this issue, you need to change the target and source properties to the java version installed in your system.