Maven Tutorial : Part 1

What is Maven?
Maven is an Apache project. Current release of version 3.3.9. Available at http://maven.apache.org/download.cgi Maven is a built tool similar to ant. Its a project management and comprehension tool. 

Pre-requisite for Maven:
Java: JDK-1.5 or above version, set JAVA_HOME path in .bashrc file.

Common problems and activities in project development:
  1. Multiple jars are required for building a project. 
  2. Lots of dependancies and version issues. 
  3. Maintain project structure (src, web.inf, pom.xml) is important. 
  4. Building, publishing, and deploying.
Maven helps to solve this problems. 

How to install Maven?
Here we learn how to install maven:
  1. Download apache maven from apache site: http://maven.apache.org/download.cgi#
  2. Extract this file: (I extracted file to Desktop)
    $tar -zxf apache-maven-x.x.x-bin.tar.gz (x - version)
  3. Move this file to /usr/local:
    $cp -R ~/Desktop/apache-maven-x.x.x /usr/local/maven
  4. Create a soft link in /usr/bin for universal access:
    $ln -s /usr/local/maven/bin/mvn /usr/bin/mvn
  5. Verify mvn installation :
    $mvn --version
Maven takes all the required information from the repositories, so to complete all the activities like dependencies, building and publishing of a project, maven required internet connection.

Steps to to use mvn:
  1. Open terminal and create a folder: $mkdir myMvnTest
  2. Go in to that folder: $cd myMvnTest
  3. Enter command : $mvn archetype:generate (archetype is how you want your application. For first time it download all required plug-in)
  4. It ask for version number. I choose 6. 
  5.  Then it ask for groupId, artifactId, version, pkg.
  6.  After selecting "Y" maven again download some required information from repository and shows following msg "BUILD SUCCESS"
  7. After Build Success it creates "MavenTestApp" folder inside that folder pom.xml file is created.
  8. Go into folder where pom.xml is present and enter the following command: $ mvn compile (mvn take care of downloading dependencies mentioned in pom.xml)
  9. Once compilation is done successfully create jar using command : $ mvn package which creates executable jar.
     
     

  10.  Execute that jar using command :  java -cp target/MavenTestApp-1.0-SNAPSHOT.jar org.kishor.testMaven.App 
  11. Output is like :  

    Thats all for this article hopes you are clear with what is exactly "MAVEN" and how useful in building project.

1 comments:

Post a Comment