What is Maven?
Apache Maven is project management tool which
is following the concept of a project object model (POM), The POM file to manage project’s build,
dependency and documentation. The most powerful feature is able to download the
project dependency libraries automatically
Key features of Maven
Apache Maven can be used in environments
where common build tools like GNU Make or Apache Ant were used. The key
features of Maven are:
- Convention over
configuration: Maven tries to avoid as much
configuration as possible, by choosing real world default values and
supplying project templates (archtypes).
- Dependency
management: it is possible to define
dependencies to other projects. During the build, the Maven build system
resolves the dependencies and, if needed, it also builds the dependent
projects.
- Repository:
project dependencies can be loaded from the local file system, from the
Internet or public repositories. The company behind the Maven project also
provides a central repository called Maven Central.
- Extensible via
plug-ins: The Maven build system is extensible
via plug-ins, which allows keeping the Maven core small. The Maven core
does for example not know how to compile Java source code, this is handled
by the compiler plug-in.
Maven Repository
A maven repository is a directory of packaged JAR file with
pom.xml file. Maven searches for dependencies in the repositories. There are 3
types of maven repository:
- Local
Repository
- Central
Repository
- Remote
Repository
Maven searches for the dependencies in the
following order:
Local repository then Central repository then Remote
repository.
If dependency is not found in these
repositories, maven stops processing and throws an error.
1) Maven
Local Repository
Maven local repository is
located in your local system. It is created by the maven when you run any maven
command.
By default, maven local repository is
%USER_HOME%/.m2 directory.
Update location of Local Repository
We can change the location of maven local
repository by changing the settings.xml file. It is located in
MAVEN_HOME/conf/settings.xml, for example: E:\apache-maven-3.1.1\conf\settings.xml.
2)
Maven Central Repository
Maven central repository is
located on the web. It has been created by the apache maven community itself.
The path of central repository is: http://repo1.maven.org/maven2/.
The central repository contains a lot of
common libraries that can be viewed by this url http://search.maven.org/#browse.
3)
Maven Remote Repository
Maven remote repository is
located on the web. Most of libraries can be missing from the central
repository such as JBoss library etc, so we need to define remote repository in
pom.xml file.
Maven pom.xml file
POM is an acronym for Project Object Model. The pom.xml
file contains information of project and configuration information for the
maven to build the project such as dependencies, build directory, source
directory, test source directory, plugin, goals etc.
Maven reads the pom.xml file, then executes
the goal.
Before maven 2, it was named as project.xml
file. But, since maven 2 (also in maven 3), it is renamed as pom.xml.
A multi project pom file includes a modules section.
This section tells Maven which project are part of the build.
In the build section of the pom
you can define plugins for which are needed for the build.
Elements of maven pom.xml
file
For creating the simple pom.xml file, you
need to have following elements:
Element
|
Description
|
project
|
It is the root element of pom.xml file.
|
modelVersion
|
It is the sub element of project. It
specifies the modelVersion. It should be set to 4.0.0.
|
groupId
|
It is the sub element of project. It
specifies the id for the project group. Defines a unique base name of the
organization or group that created the project.
|
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: JARs, source and binary distributions, and WARs.
|
version
|
It is the sub element of project. It
specifies the version of the artifact under given group.
|
Maven pom.xml file with
additional elements
Here, we are going to add other elements in
pom.xml file such as:
Element
|
Description
|
packaging
|
defines packaging type such as jar, war
etc.
|
name
|
defines name of the maven project.
|
url
|
defines url of the project.
|
dependencies
|
defines dependencies for this project.
|
dependency
|
defines a dependency. It is used inside
dependencies.
|
scope
|
defines scope for this maven project. It
can be compile, provided, runtime, test and system.
|
Maven life cycle
Every build follows a specified life cycle.
Maven comes with a default life cycle that includes the most common build phases like
compiling, testing and packaging.
The following lists gives an overview of the
important Maven life cycle phases.
- validate
- checks if the project is correct and all information is available
- compile
- compiles source code in binary artifacts
- test
- executes the tests
- package
- takes the compiled code and package it, for example
- integration-test
- takes the packaged result and executes additional tests, which require
the packaging
- verify
- performs checks if the package is valid
- install
- install the result of the package phase into the local Maven repository
- deploy
- deploys the package to a target, i.e. remote repository
If you instruct Maven to execute a phase, it
executes all existing phases in the pre-defined sequence until has executed the
defined phase. All relevant goals are executed during this process. A goal is
relevant for a phase if the Maven plug-in or the pom binds this goal to the
corresponding life cycle phase.
Maven Plugins
The maven plugins are
central part of maven framework, it is used to perform specific goal.
According to Apache Maven, there are 2 types
of maven plugins.
- Build
Plugins
- Reporting
Plugins
Build Plugins
These plugins are executed at the time of
build. These plugins should be declared inside the element.
Reporting Plugins
These plugins are executed at the time of
site generation. These plugins should be declared inside the element.
Maven
Core Plugins
A list of maven core plugins is given below:
Plugin
|
Description
|
clean
|
clean up after build.
|
compiler
|
compiles java source code.
|
deploy
|
deploys the artifact to the remote
repository.
|
failsafe
|
runs the JUnit integration tests in an
isolated classloader.
|
install
|
installs the built artifact into the local
repository.
|
resources
|
copies the resources to the output
directory for including in the JAR.
|
site
|
generates a site for the current project.
|
surefire
|
runs the JUnit unit tests in an isolated
classloader.
|
verifier
|
verifies the existence of certain
conditions. It is useful for integration tests.
|
What is archetype?
Archetype is the maven plugin. It creates the
project structure.
We can create a simple maven example by
executing the archetype:generate command of mvn tool.
To create a simple java project using maven,
you need to open command prompt and run thearchetype:generate command
of mvn tool.
Syntax
The syntax to generate the
project architecture is given below:
- mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid
- -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=booleanValue
Example
The example to generate the
project architecture is given below:
- mvn archetype:generate -DgroupId=com.teachtojava -DartifactId=SampleExample
- -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Note: Here, we are using
maven-archetype-quickstart to create simple maven core project. if you use
maven-archetype-webapp, it will generate a simple maven web application.
Advantages of Using Maven over Ant
- Managing
dependencies.
- Convention
over configuration – configuration is very minimal
- Multiple/Repeated
builds can be achieved.
- Focus
on automation.
- Plugin
management.
- Testing
- ability to run JUnit and other integration test suites.
- Making
the development process transparent.
- Provision
to check the status of each build.
- Avoiding
inconsistent setups.
- Standard
and uniformed infrastructure among projects.
Differences Between ANT and
Maven
Maven is a build automation tool used
mainly for java projects from apache.
Apache Ant is a Java library and
command-line tool whose mission is to drive processes described in build files.
Some of the differences as below.
Project
structure: Ant has not a
defined project convention, you can put things in any place, and later instruct
ant for know where the things are. Maven has a project conventions and has
several archetypes for predefined projects, so maven is easier because it knows
where things are if you follow the project convention.
Execution
way
Ant is a procedural tool, you have to tell it
when, what and how it has to do all the things: compile, then copy, then
package, then deploy, etc… Maven is a declarative tool, you only have to
declare your project object model (pom) and put your source code and resources
in the correct folder, maven will take care of the rest for you.
Lifecycle
management
Ant do not have a lifecycle management, you
have to declare some goals and define which of those goals run first, what run
later and so on manually. Maven has a lifecycle management.
Dependency
management
Ant does not have any mechanism to manage
dependencies, you have to manage it manually: download all dependencies, place
the dependencies in a folder and later copy it to the packaged artifacts, maven
has a mechanism to manage dependencies, you have a local repository that acts
like a cache and you can define some remote repositories in order to download
more dependencies, you only have to define all your dependencies needed and
maven will download it for you.
No comments:
Post a Comment