Repository Utilization In Maven (Part-3)
Dependency management is out-of-box feature provided by maven and it do so with the help of repository as I have explained in my previous article. We must be excited that how maven make the use of such repository for managing large number of open-source dependencies and how it all works??
In this article, we will learn how maven locates and resolves the dependencies defined in pom (project object model) and also how these dependencies are made available for utilization in specific project through its different repositories.
What is pom??
Let us start by knowing what is pom and what are various elements included in pom. In Maven, POM (Project Object Model) is the fundamental unit of work. It is an XML file which holds the information about the project and configuration details used to build a project by Maven. This model has a set of standards, a project life cycle, a dependency management system and logic for executing plugin goals at certain phases in the life cycle process. This file must be named as pom.xml and placed under root folder of project.
When executing a task or goal, maven reads the POM, gets the needed configuration information, then executes the goal.
Minimum configuration in pom.xml
Following are the minimum requirement for a POM.
modelVersion – should be set to 4.0.0
groupId – the id of the project’s group.
artifactId – the id of the artifact (project)
version – the version of the artifact under the specified group
GroupId, artifactId, and version are the three values that form the project’s fully qualified artifact name. This should be in the form of <groupId>:<artifactId>:<version>.
So a pom.xml contains
- project details
- packing details (jar/war/ear and etc..)
- plugin lists
- plugin executions with matching ids
- remote repository details
- dependenciesand etc.
Repository utilization in maven, how it works?
With reference to the above diagram
When we execute any maven build commands, Maven starts looking for dependency libraries in the following sequence.
1? –> Maven first searches in Local repository
1.5 –> If available –> gives jar file to app
2? –>If not available in Local repository, it will search in central repository
2.5 –> If available in central repository, it will download in local repository and gives to maven app.
3? –> If not available in central repository, it will search in remote repository by checking <repository> tag in pom.xml
4 –>If not available –> error will be raised
4.5 –> If available –> the jar will be downloaded to local repository and will be given to maven app.
I hope you understand how these dependencies are consumed via repositories in maven. If you have any queries or confusion, kindly feel free to ping me in comments section.