Saturday, August 11, 2012

What is Maven and Why to use it?

Apache Maven is a basically used for managing project's build, reporting and documentation. Maven is based on model which is called POM(Project Object Model).

Consider example that you need to use log4j for your application for logging.

In Usual way , you need to search,download and add log4j.jar into your project dependency and what if  upgraded version of log4.jar is available and you want to add into your project. You have to do this manually. Maven can assist you in order to overcome with this manual process.

In Maven, you need to know "maven coordinates" for library you want to add into your project. Ex. coordinates for log4.j which you can find at Maven Central Repository

Once you have groupId,artifactsId and version available then You should add following lines into your Project's pom.xml


<dependencies>
    <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>1.2.17</version>
    </dependency>
</dependencies>

When you build your project or compile your project, libraries are downloaded automatically to maven local repository and downloaded jar will be added to application dependency.

You can use following command to create Java Project with Maven
mvn archetype:generate -DgroupId=<Package Path> -DartifactId={project_id}
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false


Ex. mvn archetype:generate -DgroupId=com.anuj -DartifactId=MyProject
  -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Author : Anuj Patel

Blog : http://goldenpackagebyanuj.blogspot.in/