Saturday, January 19, 2013

Maven: Getting started with Apache Maven 3.0.4 (Part 2)

In the last article part 1, I have explained the steps for downloading, setting up and installation verification on both Mac and windows successfully, and if everything was going okay then you are ready for this part.

Here I will explain how to create, compile and test a basic maven project, with explanation of POM (Project Object Module), then understanding of maven build life cycle which it provides.

Creating a maven project

Now with everything set upped and all the steps has been done successfully? Great! Let's get down to the business, by creating a first maven project. Specifically with maven goal archetype:generate.
  1. First create new folder for your project, for example "C:\Hello Maven".
  2. Open new command-line on Windows or terminal on Mac and run the following command:

  3. If this is the first time you run this command you will see on command-line a bunch of downloads taking place, which used to be referenced locally by your projects after that.

  4. After downloads are finished you will see a rather long list of local or remote archetypes. Each archetype has a number, name and short description explaining the purpose of the archetype and what is it. By now we will use the default one (in my case) with the number 244 (244: remote -> maven-archetype-quickstart(An archetype which contains a sample Maven project.)).

    When being asked for the archetype number, type 244 followed by enter.

    Note: The number depends on if you are running from local or remote repositories.

    Note: If you are using internet via proxy server, then you should specifies the proxy server information according to access the internet, you provide these information via the maven settings.xml file, which is located inside maven home directory ".m2". The settings should be as the following:

  5. Then you will be asked to choose the archetype version, the default one is the most stable version lets choose that (typically choose the default in this case number 6). Types 6 then hit enter.
  6. After that, you will be asked to enter the following properties (Maven project co-ordinates) respectively:
    1. GroupId co-ordinates are used to specify the hierarchical location of a project within a Maven repository. In this case, the repository is the local Maven repository present in your filesystem. GroupId co-ordinates are typically the root package and thus can be shared by multiple projects within an organization.

    2. The artifactId is an identifier for your project and version here refers to the project version.

    3. Packages refer to the reverse DNS root package name that contains all your source and test classes.

    artifactID identifiers will be used when artifacts are deployed in repositories and used as dependencies for other projects.

    For version just hit enter and "1.0-SNAPSHOT" will be used, rather than that type what version name you want.

    After finishing all the parameters, you will get confirmation question about your entered data, choose yes and upon completion you should see the following on the command-line:

What happened just here?

We have asked a maven plugin to create a maven project for us by firing the following command "mvn archetype:generate".

A new project for apache maven can either be created by a "hand-crafted" with a minimal POM.xml file with folder creation or generated through a maven project archetype.

Surprisingly, what exactly have maven created for us????

Simple answer, the following:

It creates a maven project, with commonUI (artifactId) root project contains src and pom.xml file. The source contains the main folder which contains the project source classes, and another one test which contains all your test classes, both has the same hierarchy created as when get asked for package property Finally in main folder there is a basic application class contains hello world console app (, and its unit test class under test folder (

Compiling and testing the project

Up to this point we have created a maven project at hand; then the most interesting part is compiling and testing the previously created project.

If you are new to Apache Maven, which probably why you are here ;), then this will be your first introduction to maven build lifecycle, as maven has number of built-in lifecycle goals.

To compile:

  1. Navigate to you project folder which contains the pom.xml file, in this case commonUI, and fire the following command:
  2. After this command, Maven will begin downloading the project dependences into your local repository, and proceeds compiling your project. At the end print the following on the console:

  3. The previous command triggers the java compiler associated to the project pom.xml file, it is default to JDK 1.5+ and indeed the pom.xml can be modified to add any other JDK source to be used for compilation.
  4. The successful runs of the compile maven goal will generate a target folder, which contains all compiled source classes. the directory structure should be as the following:

To Test:

After successful compilation of your project, now it is the time for testing. Maven recognizes that software should be tested before goes to the next step, it do this as default behavior of build lifecycle with built-in goal called test. This makes TDD (Test Driven Development) implementation more easer for development teams to integrate in their development process.

When the default convention is being used in maven project, by having src\test folder that contains all tests for the code. Then just run the following command to initiate the test process:

This command triggers the test process to run all test classes under src/test folder, after successful completion you should echoed back with a brief testing report on the terminal with the following output:

And the folder structure will be as the following:
The surefire-reports contain the testing report for each test class and suite, and folder test-classes contain the compiled test unit classes. Surefire is a plugin used for testing report generation and used by default by maven.

Finally we have now reached out to the end of the article, hope you enjoy it and being useful to you, for any question just leave your comment.

No comments :

Post a Comment