Maven - Getting started

Maven is a management tool for Java and Java EE projects, allowing you to:

  • create a standard tree for the code and resources
  • download, update and configure the necessary libraries
  • compile the project, perform unit tests on the code and package the result.

Note that: The use of Maven requires an internet access and disk space to store the downloaded files.

Maven is an open source software, it is available on the official website of the Apache Software Foundation:

  • Under the names of "" for Windows and "binary.tar.gz" for Unix.
  • Once downloaded, unzip the contents of the archive into a system directory ("C:\Program Files\Apache" for example), then you must configure the MAVEN_HOME environment variable for that directory and add the \bin subdirectory to the PATH environment variable.
  • If you have not installed JDK for editing Java code, you can download it from the official site:

  • You must also configure the JAVA_HOME variable is the installation directory of the JDK, and add the \bin directory to the PATH environment variable.

In summary, you should have three environment variables with these values, for example:

JAVA_HOME   C:\Program Files\Java\jdk1.7.0_25
MAVEN_HOME  C:\Program Files\Apache\maven-3.1.0
PATH        %JAVA_HOME%\bin;%MAVEN_HOME%\bin;%PATH%

To verify that everything is installed, you can open a command prompt and type this:

mvn --version

Depending on your configuration, you will have a message like this:

Apache Maven 3.1.0 (2013-06-28 04:15:32+0200)
Maven home: C:\Program Files\Apache\maven-3.1.0\bin\..
Java version: 1.7.0_25, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.7.0_25\jre
Default locale: fr_FR, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

  • To keep things simple we will create a "Hello World" project with Maven. Create a dedicated directory for it (the location does not matter).
  • Open a command prompt and move you to the folder you have created, we'll finally get to the heart of the matter!

For now we have an empty folder, we will ask Maven to create a basic project by typing the following command:

mvn archetype:generate

Maven will then connect to the repository and download a large number of POM (Project Object Model) and JAR (Java ARchiver). These files will then be stored by default in the "My Documents" folder, in a folder named ".m2".

  • By storing these files on the disk, Maven won't need to download them again (fetch the files from the local directory).
  • Once the downloads are completed, Maven will inform you that it will build the project in interactive mode. It will ask a number of questions that will have to be answered, most often from a list of choices.

First question: choose the type of project. Maven provides a large number of archetypes, here is a sample:

823: remote -> (-)
824: remote -> (-)
825: remote -> se.vgregion.javg.maven.archetypes:javg-minimal-archetype (-)
826: remote -> sk.seges.sesam:sesam-annotation-archetype (-)
827: remote -> tk.skuro:clojure-maven-archetype (A simple Maven archetype for Clojure)
828: remote -> (-)

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 294:

Lines from 1 to 828 are all possible projects choices. In our example we shall use the project numbered as 294:

294: remote -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.)

So enter this default value to tell Maven to use the "quickstart" archetype.
The second question: choose the version of the archetype that you want to use. Again we will take the default value, which generally corresponds to the last stable version. Maven can then download the project. But it must be customized.
The next question concerns the "groupId", which represents the name of your group, for example, the one of Maven is "org.apache.maven". For this section, you can put whatever you want provided that you use expected syntax (lowercase letters without accents and no words separated by space). For example, I'll put "org.ccm.maven"
Then comes the "artifactId", which is the name of the project. Here I would enter "helloworld".
The next question concerns the version of the project. Maven has many version numbers, so it is important to understand that every time you will build your Maven project, the version number may be incremented. By stating that it is a "snapshot" version will ensure that the project will be well considered as being under development and therefore the version will not be incremented. Use the default value: "1.0-SNAPSHOT".
You will next get a question about the package of the project, by default Maven proposes to the same name as the groupId. We shall use "org.ccm.maven".
You will get a small summary of your choicces and confirm them using the "Y" key. That's it our first Maven project is created!
Note: You can achieve the same result without using the interactive mode, in which case you must specify the various required parameters and complete the missing ones:

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=org.ccm.maven -DartifactId=helloworld -DinteractiveMode=false

Here the architecture of the project as generated with maven-archetype-quickstart and the values of the parameters used in interactive mode:

| pom.xml
| src
| | main
| | | java
| | | | org
| | | | | ccm
| | | | | | maven
| | | | | | |
| | test
| | | java
| | | | org
| | | | | ccm
| | | | | | maven
| | | | | | |

We have 12 folders and 3 files, which are as follows:

helloworld/src/main/java/org/ccm/maven/ :

package org.ccm.maven;

 × Hello world!
public class App 
    public static void main( String[] args )
        System.out.println( "Hello World!" );

Nothing complicated so far, Maven gives us the code of a classic Hello World.

helloworld/src/main/java/org/ccm/maven/ :

package org.ccm.maven;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

 × Unit test for simple App.
public class AppTest 
    extends TestCase
     × Create the test case
     × @param testName name of the test case
    public AppTest( String testName )
        super( testName );

     × @return the suite of tests being tested
    public static Test suite()
        return new TestSuite( AppTest.class );

     × Rigourous Test :-)
    public void testApp()
        assertTrue( true );

In this case, it is a bit more complicated if you do not know JUnit. JUnit is a test class unit, allowing you to automatically perform test your code after each compilation.
Note: here the test is bogus, it simply checks that true is true, which is obviously correct.
Finally, here is the core of Maven, which contains all the configuration of the project:


<project xmlns="" xmlns:xsi=""





We find the different values ??defined in our project and dependencies to allow JUnit tests. By configuring this dependency in the pom, Maven will automatically download the JUnit library and integrate it to the project.
We can also see the packaging tag, which is a jar. This means that after compiling the code, it will be archived in a JAR file.
However it lacks a little something, indeed it is not executable jar! We will thus add the entry point by copying this block of code in the pom.xml file (e.g between the url and properties)


Now that our source code and the configuration of the project is complete, we can tell Maven to build the project. To do this, open a command prompt in the "helloworld" folder and enter this command:

mvn package

This will trigger several actions:

  • validation of the project
  • compilation of the project,
  • the execution oof unit tests and creation of jar archives.

Note that if any of these steps fail, then the following will not be executed.
A new folder appeared in your helloworld directory, it is "target" folder, it contains the compiled classes, test reports ("surefire-reports"), as well as the "helloworld-1.0-SNAPSHOT.jar" archive and compilation properties.
So now you can start the program with the command:

java -jar target/helloworld-1.0-SNAPSHOT.jar

The result will be as expected: Hello World!

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Maven - Getting started », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (