Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Sunday, January 24, 2010

Maven Basics

Coming from the Ant world it took sometime for me to digest the basics of Maven and it is difficult to get into any tool or technology without getting its basics. So I am putting down my thoughts/understanding of maven for newbies.Also I request to point out and correct any mistakes as I am not an expert of Maven.

Why Maven and how is it different from Ant?
Maven is a standardized project development and lifecycle management system
for java projects.The problem with Ant was that each project will follow its own way of building and packaging applications and this lack of standard or convention will be a problem when switching from one project to another or sharing the libraries among projects.So a new concept of standardizing and building some patterns for different kind of projects came up which will help people to reuse the build models and libraries among projects.The basic concept of maven are described below.

1)Standard build structure
Maven enforces a standard build structure for all the java projects.Please refer the reference link to understand the same.

Reference :
http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
2) Project Object Model or POM
Each project need to have an xml file(pom.xml) that contains information about the project and configuration details used by Maven to build the project.This should be placed in the root directory of the project.
Reference : http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

3) Concept of central repository for libraries
This is useful when you have multiple projects using the same set of libraries and want
to share them.Also it makes version control and dependancy management easy.The libraries will be made vailable in central repositories online and you specify the details of the libraries as dependencies in the pom.xml.You can find the libraries from either the central repository of maven ()or from any other Maven repositories available. When we build the system the the dependant jars will be downloaded from the net and will be put into your local
repository.You can specify the location of your local repository in M2_HOME/settings.xml or it will take a default location(For windows it is My Documents/.m2 and for linux ~home/.m2). So instead of each project maintaining separate libraries the libraries will be taken from the local repostory while building.The reference links have more information about repositories.

Reference:
http://maven.apache.org/guides/introduction/introduction-to-repositories.html
http://www.mkyong.com/maven/how-to-search-the-maven-coordinates-pom-xml-dependency/

4) Maven plugins and goals

Maven accomplishes everything using plugins .Intact Maven can be considered as a plugin
execution framework.We will crate a plugin for each task you want to do with maven.Each plugin will again have a set of goals.Maven comes with a standard set of plugins and goals for common project management tasks.
For eg.
clean -Clean up after the build.
site- Generate a site for the current project.

In addition to that you can create your own plugins to accomplish the tasks specific to your project.For eg.Scala has a maven plugin to help managing scala projects.Again plugin will be available in central repositories(http://repo1.maven.org/maven2/org/apache/maven/plugins for maven) .If you are using the libraries or plugins from maven central repository you need not specify the repositories and plugin repositories in pom.xml files.Any other repositories should be specified in pom.xml.

Reference :

http://maven.apache.org/plugins/index.html
http://hamandeggs.wordpress.com/2009/08/30/creating-a-simple-maven-plugin/

5) Maven archetype plugin

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made. Archetype will help authors create Maven project templates for users, and provides users with the means to generate parameterized versions of those project templates.

To create a new project based on an Archetype, you need to call mvn archetype:generate goal as follows

mvn archetype:generate

It will list the archetypes available in the repositories available and you can choose the one which fits your project .It will ask for some more information about the project like name and version and once you provide those details archtype lugin will create the ptoject structure and pom.xml file for your project.Refer the links for more information


References
http://www.mkyong.com/maven/how-to-create-a-project-with-maven-template/
http://maven.apache.org/guides/introduction/introduction-to-archetypes.html