Maven
Maven, is an yiddish word meaning accumulator of knowledge.
It was a by product of Jakarta Turbine project built under Apache.
It's an open source project written in java.
It is a build automation tool for overall project management.
It helps in
Checking a build status
Generating reports (basically javadocs).
Setting up the automated build process and monitors the same.
It follows convention over configuration philosophy.
Maven's objective
Making the build process easy.
Providing a uniform build system.
Providing quality project information.
Encouraging better development practices.
Maven's purpose
Maven provides following for a project,
Build tooling
Version management
Re-usability
Maintainability
Comprehensibility
Inheritance
Maven also provides following capabilities,
Produce different build target and result builds via profiles
Test code
Generate documents
Furnish metrics
Deploy build artifacts
Why Maven?
It eases out source code compilation, distribution, documentation, collaboration with different teams.
Maven tries to describe
How a software is built.
The dependencies, plug-ins & profiles that the project is associated in a standalone or a distributed environment.
Working of Maven
Maven uses a Project Object Model (POM) to manage a project.
Maven commands execute parts of its Project Object Model.
POM
A POM is usually described in XML document. (Other formats can be used to describe the Project Object Model, however, XML was the first format used.)
It describes build management needs for a project. Some of them are listed below
Project NeedsDescriptionproject coordinates
Uniquely identifiable set of properties by which the project artifacts can be consumed elsewhere.
dependencies
Libraries and code needed to execute the project build management
plugins
Helper tools that execute build and build management aspects
properties
Common and extracted values used in the project
inheritance details
The ability to create a hierarchy of re-usable POM components
profiles
Alternate execution pathways that can be activated on a per-execution basis
Some elements (xml tags) of maven
pom.xml
fileTagsDescriptionproject
It is the root element of pom.xml file.
modelVersion
It is the sub element of project. It specifies the modelVersion.
groupId
It is the sub element of project. It specifies the id for the project group (typically organization name).
artifactId
It is the sub element of project. It specifies the id for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include
JAR
,WAR
etc.version
It is the sub element of project. It specifies the version of the artifact under given group.
packaging
Defines packaging type such as jar, war etc.
name
Defines name of the maven project.
plugins
Compiler plugins, eclipse plugins.
dependencies
Collection of dependencies for this project.
scope
Defines scope for this maven project. It can be
compile
,provided
,runtime
,test
andsystem
.Maven utilizes content in the
POM
for its build management. However, maven also has convention-based defaults. Maven thus has the onus of amalgamating defaults and applying overrides and additions discovered in the project’s pom file (typically a pom.xml).This amalgamation of defaults and applying overrides and additions results in an effective POM.
Effective POM
An assembly of execution steps, properties and profiles.
The content that maven can execute for the project.
An exhaustive set of dependencies and plugins needed for such an execution.
Determination of any transitive dependencies (dependencies of dependencies, full depth imaginable).
Any conflict resolution in terms of dependency versions.
An effective
POM
is a combination of following.
More about layers in the above diagram can be read here.
Maven coordinates
A way to uniquely identify an artifact. There are three primary coordinates that are used to identify an artifact.
groupId
artifactId
version
Lifecycle
A lifecycle groups a sequence of activities pertaining to a specific type of build-management.
There are 3 basic lifecycles for its standard build management however, more lifecycles can be created as needed.
Lifecycle comprises of phases and maven commands are typically execution of phases.
Standard Lifecycle
clean
Intended for clean-up of any prior build-managed outputs and artifacts.
default
(build)
Intended for project build, test and deployment of artifacts.
site
Intended for project site documentation.
Phases
Phases are executable blocks.
Phases follow an ordered sequence within a given lifecycle.
Goals
They are bound to phases and are unit of work.
This is aka a binding.
A goal performs a task that is considered relevant for the given lifecycle and phase. Maven provides some built in goals. Goals are defined in plugins.
Every packaging comes with a predefined set of goals bound to phases of the default lifecycle.
Some goals may not be bound to anything and can be invoked directly without a phase-binding.
Plugins
Plugins are maven’s way of defining goals and providing connectors for the goals to phases.
Plugins are developed as MOJOs _(Maven’s plain Old Java Objects). The plugins define goals and supply logic to deliver the goals.
Goals are usually bound to phases in either the maven built-in configurations or the project POM file.
Maven Hierarchies
Three types of Maven hierarchies are supported,
Parentage
A parent POM is a POM from which the current project POM can inherit content.
The project POM can depend on exactly one parent POM. This single-parent inheritance is one-way. The parent POM is unaware of the POM that inherits from it.
The child POM declares the parentage in its own
pom.xml
.
Aggregation
An aggregator POM (also known as a reactor POM) is a POM that can sequence the builds of many projects.
An aggregate POM specifies all the projects that can be build-managed together.
The child POM(s) remain unaware of the aggregator POM that invokes it. A child POM can be a listed in more than one aggregator POM.
The aggregator POM lists the child POM by name in it’s own
pom.xml
as a module. As the declared module suggests, this pattern is for modular builds of projects. There is no inheritance of any content from the aggregator POM.
Bill of Materials
A bill-of-materials POM is a POM that can declare bundles of dependencies that have been tested to work well together.
The abundance of artifacts and versions of each can, at times, lead to confusion and needs for trial-and-error mechanisms to determine compatibility and/or right functionality. A bill-of-materials POM reduces that overhead.
A bill-of-materials POM is a means of multiple inheritance too, since a project POM can import multiple bill-of-material POMs.
Central Repositories
A server where maven hosts resuable dependency (library) artifacts
Maven's primary repository is called Maven Central.
Ant disadvantages
While using ant , project structure had to be defined in build.xml. Maven has a convention to place source code, compiled code etc. So no need to provide information about the project structure in pom.xml file.
Maven is declarative, everything you define in the pom.xml file. No such support in ant.
There is no life cycle in Ant, where as life cycle exists in Maven.
Maven advantages
Managing dependencies
Uses Convention over configuration - configuration is very minimal
Multiple/Repeated builds can be achieved.
Plugin management.
Testing - ability to run JUnit and other integration test suites.
What is POM? (Project Object Model)
It is the core element of any maven project. Any maven project consists of one configuration file called pom.xml. Location --In the root directory of any maven project.
It contains the details of the build life cycle of a project.
Contents Dependencies used in the projects (Jar files) Plugins used Project version Developers involved in the project Build profiles etc.
Maven reads the pom.xml file, then executes the goal.
Goals in Maven Goal in maven is nothing but a particular task which leads to the compiling, building and managing of a project. A goal in maven can be associated to zero or more build phases. Only thing that matters is the order of the goals defined for a given project in pom.xml. Because, the order of execution is completely dependent on the order of the goals defined. eg : clean , build ,install ,test
What is a Maven Repository
A maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies(JARs) in the repositories. There are 3 types of maven repository:
Maven searches for the dependencies in the following order:
Local repository then Central repository then Remote repository. maven repositories
If dependency is not found in these repositories, maven stops processing and throws an error.
Maven Local Repository
Maven local repository is located in the file local system. It is created by the maven when you run any maven command.
By default, maven local repository is HOME / .m2 directory. (Can be updated by changing the MAVEN_HOME/conf/settings.xml)
Maven Central Repository
Maven central repository is located on the web(Created by the apache maven community)
The path of central repository is: https://mvnrepository.com/repos/central
Maven Remote Repository
Maven remote repository is also located on the web. Some of libraries that are missing from the central repository eg JBoss library , Oracle driver etc, can be located from remote repository.
Maven Build Life Cycle What is it ? The sequence of steps which is defined in order to execute the tasks and goals of any maven project is known as build life cycle in maven.
Maven comes with 3 built-in build life cycles
Clean - this phase involves cleaning of the project (for a fresh build & deployment) Default - this phase handles the complete deployment of the project Site - this phase handles the generating the java documentation of the project.
Build Profiles in Maven
It is a subset of elements which allows to customize builds for particular environment. Profiles are also portable for different build environments.
Build environment basically means a specific environment set for production and development instances. When developers work on development phase, they use test database from the production instance and for the production phase, the live database will be used.
So, in order to configure these instances maven provides the feature of build profiles. Any no. of build profiles can be configured and also can override any other settings in the pom.xml
eg : profiles can be set for dev, test and production phases.
Installation (w/o IDE)
Download Maven from Apache (version 3.x)
Add MAVEN_HOME as environment variable
Add maven/bin under path (for easy accessibility) 4.Verify maven mvn -- version
OR use m2e plug-in (a standard part of Eclipse for J2EE)
Last updated