Friday, September 4, 2015

INTRODUCTION TO MAVEN

 Maven is a popular open source build automation tool and project management tool. It is designed to help us build the projects in a standard way with an easy way to publish project information and share JARs across several projects. It basically takes much of hard work out of build process.
Basically maven will help in -


  • Compiling your classes
  • Preparing a package out of these compiled classes
  • Running your tests
  • Deploying into the server

So all you have to do is follow certain conventions set by maven. Following is the standard Maven2 directory layout-

The src directory has a number of sub-directories, each of which have clearly defined  purpose:

  • src/main/java            : Your Java source code goes here.                                                 (for JAR files)                        
  • src/main/resources  :Other resources your application needs                                         like xml files and configurations                                                      goes here.(for WAR files)                                                                                     
  • src/main/filters         :Resource filters, in the form of properties                                       files, which may be used to define                                                 variables only known at runtime                                                                
  • src/main/webapp     :The Web application directory for a WAR                                      project                             
  • src/test/java             :Here the unit tests are carried out                                           
  • src/test/resources   :Resources to be used for unit tests, but will                                   not be deployed                                                                                      
  • src/site                      :Files used to generate the Maven project Website.


Once we follow this convention, Maven will automatically compile our classes, bundle those classes in jar files and run our unit test before deploying it in the server. So, it copies all its compiled classes and resources in the target directory which is shown in the layout above.

pom.xml file is Project Object Model(POM) which is the heart of Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. 

This is how a pom.xml file should look like-


  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.mycompany.app</groupId>
  7. <artifactId>my-app</artifactId>
  8. <packaging>jar</packaging>
  9. <version>1.0-SNAPSHOT</version>
  10. <name>Maven Quick Start Archetype</name>
  11. <url>http://maven.apache.org</url>
  12. <dependencies>
  13. <dependency>
  14. <groupId>junit</groupId>
  15. <artifactId>junit</artifactId>
  16. <version>3.8.1</version>
  17. <scope>test</scope>
  18. </dependency>
  19. </dependencies>
  20. </project>
It includes the informations like version of object model (<modelVersion>4.0.0</modelVersion>) this POM is using, your group Id and artifact Id for the unique identifier of the organization or group, packaging type to be used by this artifact(e.g. JAR, WAR, EAR etc.) and The url indicating where the project's site can be found.

You can declare your dependencies here. Suppose your project depends on another project, you can simply add those dependencies and frameworks in pom.xml and Maven will automatically pull those dependencies. 

To make it clearer, when you build a Maven's project, Maven will check your pom.xml File to identify which dependency to download. Here, Maven will get the dependency from your local repository, if not found, then get it form default Maven central repository http://repo1.maven.org/maven2/.

WHAT IS MAVEN LOCAL REPOSITORY?

Maven local repository is simply a local folder where all the project's dependencies (plugin jars and files) downloaded by Maven is stored. In windows by default it's location is in-
Windows – C:\Documents and Settings\{your-username}\.m2

                                                                                                              



No comments:

Post a Comment