maven logo

Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software:

  1. It describes how software is built.
  2. It describes its dependencies.

Maven uses conventions for the build procedure, and only exceptions need to be written down. Maven is built using a plugin-based architecture that allows it to make use of any application controllable through standard input. The following tutorial will show you how to download, install and configure Apache Maven 3.2.2 on Windows.

Maven Install

Before starting this tutorial, make sure that a Java runtime environment (JRE) is installed and configured on your system. A quick way to check this is by opening a command prompt and executing following statement which prints the installed JRE, if correctly installed.

java -version

Head over to the Maven download page and locate Files section. Download the binary ZIP file, at the time of writing it was apache-maven-3.2.2-bin.zip. This version is still available in the archives section of the Maven site. In this tutorial we will install on Windows.

maven download page

Extract the binaries archive downloaded in the previous step. The extracted root directory should contain a number of files and subdirectories as shown below. From now on we will refer to this directory as: [maven_install_dir].

maven install location

Maven Configuration

Next we need to setup a 'M2_HOME' environment variable that will point to the installed Maven runtime. In addition if we want to run Maven from a command prompt we need to setup the 'PATH' environment variable to contain the Maven bin directory.

When using Windows the above parameters can be configured on the Environment Variables panel. Click on the Windows Start button and enter “env” without quotes as shown below.

edit environment variables

Environment variables can be set at account level or at system level. For this example click on Edit environment variables for your account and following panel should appear.

environment variables

Click on the New button and enter “M2_HOME” as variable name and the “[maven_install_dir]” as variable value. In this tutorial the installation directory is 'C:\source4code\tools\apache-maven-3.2.2'. Click OK to to save.

Note that “M2_HOME” is used for Maven 2 and later. “MAVEN_HOME” is for Maven 1.

maven home user variable

Select the 'PATH' entry and click on the Edit button. Add “;%M2_HOME%\bin” at the end of the variable value and click OK to save.

Note that in case a 'PATH' variable does not exist you can create it and use “%M2_HOME%\bin” as the variable value.

maven path user variable

The result should be as shown below. Click OK to close the Environment Variables panel.

environment variables maven configuration

In order to test the above configuration, open a command prompt by clicking on the Windows Start button and typing “cmd” followed by pressing ENTER. A new command prompt should open in which the following command can be entered to verify the installed Maven version:

mvn -version

The result should be that the Maven version is printed as shown below.

maven version command

Maven Usage

Let’s finish the tutorial by creating a basic Maven HelloWorld project. Open a command prompt and navigate to the directory in which you want to create the project. In the example below we will use C:\source4code\code. Next enter following Maven command and press ENTER.

mvn archetype:generate -DgroupId=info.source4code -DartifactId=hello-world -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Maven will start looking for the needed dependencies in order to create the project which is based on the 'maven-archetype-quickstart' archetype (= a Maven project templating toolkit). If needed, dependencies are downloaded to the local repository. Once all dependencies are resolved, the project is created and a 'BUILD SUCCESS' statement is shown.

Feel free to browse the created hello-world directory. At the root you should be able to find the pom.xml, which is the XML representation of the Maven project.

maven create new project

This concludes the setting up and configuring Maven. If you found this post helpful or have any questions or remarks, please leave a comment.

Leave a Comment