Monday, February 15, 2016

Maven Basics

When you have maven project and maven build system in your machine, it is important to understanding how the whole system works. Maven is built with principle of convention over configuration. So, knowing how maven works means knowing:
  1. What is the Workflow of maven project?
  2. How project knows how to get dependencies?
  3. How project manage the dependencies?
  4. Where those dependencies are stored?
  5. What does it means for the project when maven settings has different kinds of repositories and update policies?
  6. Where is this settings file located?
  7. What are the minimal configuration needed for maven to work?
  8. etc...
This all started when I got confused about how the whole system is working when your settings.xml has repository with update policy defined as "always" and how it effects the application using such dependencies. So, I wanted to get deep into this.

Basics

After you install the maven 3, you need to make sure you have two environment variable defined: M2_HOME(pointing to maven installation directory) and JAVA_HOME(pointing to JDK installation directory). Also add Maven bin folder($M2_HOME/bin) in your PATH.
Now you can have maven project working in your machine. Any maven project will have pom.xmlin its root directory. This is the file that contains information about the project and configuration details used by Maven to build the project. For instance, if your project depends on library say, junit, just add the following within the dependencies tag.
 <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
 </dependency>
Now, as soon as you trying to compile the project, saying using mvn compile, maven will try to find this library from default central repository http://repo1.maven.org/maven2. Now how maven knows that? Every pom.xml in the project extends the super pom.xml resided at Maven folder inside lib/maven-model-builder-3.0.3.jar:org/apache/maven/model/. This super pom has reference to the central repository. Once the library is found in the central repository, it will be downloaded into your local repository and your project reference to this local repository. Also understand if your local repository already has this library, it will not check in central repository. Know that library has to be exact match as defined in pom file, its groupId, artifactID and Version. This is how every single libraries are recognized uniquely in repository.
Now, how your project knows where is the local repository? In your maven installation folder there is a settings.xml file inside conf folder. This is a global file where all applications find where the location of local repository is(if the local repository is not defined, default location it assumes is users-home-directory/.m2), where alternative remote repositories are, what authentication information are if any needed to access remote repositories, etc. This is not the file you should carry with your application, however, if your application needs specifics settings like, authentication information and repositories and you don't want to pollute this global settings.xml, you can create one and reference to its location when running your application. In fact, maven expect such custom settings.xml file at users-home-directory/.m2 folder by default and relationship with global settings.xml is similar to pom with super pom.
Please checkout Apache maven docs and also know about all elements you can use onsettings.xml

No comments: