Maven Tutorial in 7 Minutes


Maven is a project management tool which makes it easy to manage :
-> Builds
-> Dependencies
-> Documentation
-> Reporting
-> Releases
-> Distribution etc

Before moving further , lets look at some terminology used in Maven.

Maven Terminology

Project Object Model (POM) : It is the meta data or configuration file ( pom.xml ) for Maven Project. It is located in root directory of each maven project.

Artifact : It is something that is produced or used by the Maven project. Examples are JARs, Source and Binary distributions and WARs. Each artifact is uniquely identified by its groupID and artifactID which is unique within group.

GroupId : A group ID is a universally unique identifier for a project.

Dependency : Dependencies are libraries such as JAR artifacts which are used by Project.

Plug-in and Goal : Every piece of functionality in Maven is provided by a plug-in. Plug-in contains Goals ( which can be thought of as tasks ) and uses meta data provided in POM to perform their tasks.

Repository : It is used to hold artifacts or dependencies of projects. There are two types of repositories in Maven which are 1) Local Repository ( which is folder on your local computer ) 2) Remote Repository

Now lets get into practicals :

Step 1) Install Maven
Step 2) Basic Example on creating a project with Maven
Step 3) Basic Operations of Maven

Install Maven

Here is the 2-Step Maven Installation which you can complete in a minute.

Basic Example on creating a project with Maven

Open Command Prompt and change current directory to some other directory of your choice where created project will be stored.

For Example :

C:\Users\Prashanth>cd G:\My Workplace\lab\maven 
G:\My Workplace\lab\maven>

Type the following command ( with given parameters ) and press Enter

mvn archetype:generate -DgroupId=com.codeforeach -DartifactId=Basic-Example-by-CodeForEach 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Command Explained :

mvn                                          =    Maven Command
archetype                                 =    Plugin provided by Maven
generate                                   =    Goal in 'archetype' plugin ( generates the project based on the given template )
-D...........                                    =    these are the parameters to the goal 'generate'
maven-archetype-quickstart    =    Template provided by Maven

Since you are running this command for the first time, based on your network connection speed it takes some time for downloading the required artifacts of maven into the local repository. At the End, it stops with the following output.

[INFO] Scanning for projects...
4/4 KB
[INFO] Parameter: groupId, Value: com.codeforeach
[INFO] Parameter: packageName, Value: com.codeforeach
[INFO] Parameter: package, Value: com.codeforeach
[INFO] Parameter: artifactId, Value: Basic-Example-by-CodeForEach
[INFO] Parameter: basedir, Value: G:\My Workplace\lab\maven
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: G:\My Workplace\lab\maven\Basic-Example-by-CodeForEach
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:42 min
[INFO] Finished at: 2014-11-18T23:34:15+05:30
[INFO] Final Memory: 15M/104M
[INFO] ------------------------

This will create the project with following Standard Directory Structure.

Basic-Example-by-CodeForEach Folder Structure

Basic Operations of Maven

Lets look at three basic operations : compiling, creating the jar for above project and creating a documentation site for the project.

First you need to change the current directory to the created project folder.

cd Basic-Example-by-CodeForEach

1) Compile the Project

command : mvn compile

This will compile the project ( only main source files )

Output files location : target/classes

2) Build the Project

command : mvn package

This will build( creates either jar or war file which depends on the packaging info provided in POM ) the project .

For above project we created, it generates a jar file since package value is jar in POM ( pom.xml ) of the project.

Output file location : target/<jar file>

3) Create a Documentation Site

command : mvn site

This operation creates a site for the project based upon the information provided in the POM.

Output files location : target/site


References :



Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>