Saturday, June 28, 2014

Maven - Install Apache Maven on Windows

maven logo
Maven is a build automation tool used primarily for Java projects. Maven addresses two aspects of building software: First, it describes how software is built, and second, 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 setup and configure Maven on your computer.


The tutorial assumes a Java runtime environment (JRE) has been installed and configured on your computer. If not please check the following post on how to install and configure a JRE.

Maven Install

Start by going to the Maven download page and locate the current stable version of Maven section. Download the binary ZIP file, at the time of writing it was 'apache-maven-3.2.2-bin.zip'. 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 directory

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 for windows account

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 panel

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.
add m2_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.
edit path variable value

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

environment variables panel 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 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 archetype example


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

No comments:

Post a Comment