Maven - Introduction
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.
Download and installation
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:http://maven.apache.org/
- Under the names of "binary.zip" 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
To verify that everything is installed, you can open a command prompt and type this:
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"
Hello World with Maven
- 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!
Generate the Archetype
For now we have an empty folder, we will ask Maven to create a basic project by typing the following command:
Maven will then connect to the http://repo.maven.apache.org/
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 -> ru.nikitav.android.archetypes:release (-)
824: remote -> ru.nikitav.android.archetypes:release-robolectric (-)
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 -> uk.ac.rdg.resc:edal-ncwms-based-webapp (-)
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
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
The Project Tree
Here the architecture of the project as generated with maven-archetype-quickstart and the values of the parameters used in interactive mode:
| | main
| | | java
| | | | org
| | | | | ccm
| | | | | | maven
| | | | | | | App.java
| | test
| | | java
| | | | org
| | | | | ccm
| | | | | | maven
| | | | | | | AppTest.java
We have 12 folders and 3 files, which are as follows:
× 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.
× Unit test for simple App.
public class AppTest
× 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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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)
Compiling and running the program
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:
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!
Published by jak58
Latest update on August 22, 2013 at 08:29 AM by jak58.