Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

 Maven is a powerful project management tool that is based on POM (project object model).

 It is used for projects build, dependency and documentation.


 It simplifies the build process like ANT.
 In short terms we can tell maven is a tool that can be used for building and managing any
Java-based project.
Some significant benefits of Maven are as following

 Maven has enhanced dependency management.

 In Maven, there is no need to store the binary libraries (third party) within the source
control.

 It efficiently manages the hierarchical dependency tree.

 It makes the debugging process more straightforward.

 It provides better co-operation among the source code, plugin, libraries, and the IDE.

 It reduces the duplication within the project.

key features of Maven

 Simple project setup that follows best practices


 Consistent usage across all projects
 Easy way to build projects in which unnecessary details are hidden
 Uniform build system, where a standard strategy is followed
 Dependency management including automatic updating
 Quality project information, such as dependency lists, cross referenced sources and unit
test reports
 Dependency management, including automatic updating and dependency closures
 The ability to handle multiple projects simultaneously
 Dynamic downloading of necessary Java libraries and plug-ins from Maven repositories

Advantages of using Maven

 Maven can add all the dependencies required for the project automatically by reading
pom file.
 One can easily build their project to jar,war etc. as per their requirements using Maven.
 Maven makes easy to start project in different environments and one doesn’t needs to
handle the dependencies injection, builds, processing, etc.
 Adding a new dependency is very easy. One has to just write the dependency code in
pom file

What maven does

 We can easily build a project using maven.


 We can add jars and other dependencies of the project easily using the help of maven.
 Maven provides project information (log document, dependency list, unit test reports etc.)
 Maven is very helpful for a project while updating central repository of JARs and other
dependencies.
 With the help of Maven we can build any number of projects into output types like the JAR,
WAR etc without doing any scripting.
 Using maven we can easily integrate our project with source control system (such as
Subversion or Git).

When should someone use Maven

 When there are a lot of dependencies for the project. Then it is easy to handle those
dependencies using maven.
 When dependency version update frequently. Then one has to only update version ID in
pom file to update dependencies.
 Continuous builds, integration, and testing can be easily handled by using maven.
 When one needs an easy way to Generating documentation from the source code,
Compiling source code, Packaging compiled code into JAR files or ZIP files.
Maven Repositories

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:

1. Local Repository
2. Central Repository
3. Remote Repository

Maven searches for the dependencies in the following order:


Local repository then Central repository then Remote repository.

 Local Repository: Local repository is a directory on the developer's device. The local
repository contains all of Maven's dependencies. Even though several projects rely on
dependencies, Maven only needs to download them once.
 Central Repository: The Maven community has built the central Maven repository. Maven
searches this central repository for any dependencies that aren't available in your local
repository. The dependencies are subsequently downloaded into your local repository by
Maven.
 Remote Repository: Maven may download dependencies from a remote repository hosted
on a web server. It is frequently used to host internal organization projects. The
dependencies are subsequently downloaded into your local repository by Maven

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.
Element Description

project It is the root element of pom.xml file.

groupId a unique base name of the company or group that created the project

artifactId a unique name of the 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 a version of the project. It specifies the version of the artifact under given group.

How Maven works


Maven Build Lifecycles

 Every Maven build follows a specified lifecycle.

 we can execute several build lifecycles goals, including the ones to compile the project’s
code, create a package, and install the archive file in the local Maven dependency
repository.

Lifecycle Phases
The following list shows the most important Maven lifecycle phases:

 validate – checks the correctness of the project


 compile – compiles the provided source code into binary artifacts
 package – packages compiled code into an archive file
 test – executes unit tests
 verify – checks if the package is valid
 install – installs the package file into the local Maven repository
 deploy – deploys the package file to a remote server or repository
Maven goals

 Maven is based around the central concept of a Build Life Cycles.


 Inside each Build Life Cycles there are Build Phases, and inside each Build Phases there
are Build Goals.
What are Maven Plugins

 Maven is actually a plugin execution framework where every task is actually done by
plugins.
Maven Plugins are generally used to –
 create jar file
 create war file
 compile code files
 unit testing of code
 create project documentation
 create project reports

Project directory structure

The directory structure of a normal idiomatic Maven project has the following directory entries:
The directory structure of a normal idiomatic Maven project has the following directory entries:

project home Contains the pom.xml and all subdirectories.

src/main/java Contains the deliverable Java source code for the project.

src/main/resources Contains the deliverable resources for the project, such as property files.

Contains the testing Java source code (JUnit or TestNG test cases, for
src/test/java
example) for the project.

src/test/resources Contains resources necessary for testing.

The target folder is the maven default output folder. When a project is
build or packaged, all the content of the sources, resources and web files
Target
will be put inside of it, it will be used for construct the artifacts and for
run tests

Artifact in maven
 Artifact: An artifact is something that is either produced or used by a project.
 Artifact is the final application which has created from source code in the form of .JAR file
 Examples of artifacts produced by Maven for a project include: JARs, source and binary
distributions, WARs. Dependency: A typical Java project relies on libraries to build and/or
run. Those are called “dependencies” inside Maven
Jenkins Maven

Jenkins is an open-source continuous A Maven is a build tool which helps in build and
integration software tool. It was written for version control.
testing and reporting.

Jenkins came in year 2011. Maven was introduced early in 2004.

Jenkins is used by companies Netflix, Maven is used by companies Zillow, Intuit,


Facebook, eBay, LinkedIn, etc. Zalando, Yammer, etc.

Jenkins is integrated with Slack, Datadog, Maven is integrated with tools Buddy, Flyway,
BrowserStack, etc. tools. JitPack, SonarQube, etc.
Gradle Maven

It is a build automation system that uses a It is a software project management system that is
Groovy-based DSL (domain-specific language ) primarily used for java projects.

It does not use an XML file for declaring the It uses an XML file for declaring the project, its
project configuration. dependencies, the build order, and its required plugin.

It is based on a graph of task dependencies It is based on the phases of the fixed and linear model.
that do the work.

In Gradle, the main goal is to add functionality In maven, the main goal is related to the project phase.
to the project.

It avoids the work by tracking input and It does not use the build cache; thus, its build time is
output tasks and only runs the tasks that have slower than Gradle.
been changed. Therefore it gives a faster
performance.

Gradle is highly customizable; it provides a Maven has a limited number of parameters and
wide range of IDE support custom builds. requirements, so customization is a bit complicated.

Gradle avoids the compilation of Java. The compilation is mandatory in Maven.

Ant Maven

Ant doesn't has formal conventions, so we Maven has a convention to place source code,
need to provide information of the project compiled code etc. So we don't need to provide
structure in build.xml file. information about the project structure in
pom.xml file.

Ant is procedural, you need to provide Maven is declarative, everything you define in
information about what to do and when to do the pom.xml file.
through code. You need to provide order.

There is no life cycle in Ant. There is life cycle in Maven.

It is a toolbox It is a framework

It is mainly a build tool It is mainly a project management tool

The ant scripts are not reusable The maven plugins are reusable

It is less preferred than Maven It is more preferred than Ant


How integrate maven with the Jenkins

Configure Maven in Jenkins


1. Go to Jenkins Dashboard ->Manage Jenkins ->Manage plugins ->Available ->Maven
Integration ->Install.

2. Go to Manage Jenkins->Global tool configuration->Maven -> Add Maven_home variable


value (i.e. path of the maven file on your system)
can we use maven for testing
 Maven is used to define project structure, dependencies, build, and test management.
 Using pom. xml(Maven) you can configure dependencies needed for building testing and
running code.
 Maven automatically downloads the necessary files from the repository while building the
project

Maven Commands
 mvn clean. This command cleans the maven project by deleting the target directory

 mvn compiler:compile. This command compiles the java source classes of the maven
project

 mvn compiler:testCompile

 mvn package

 mvn install

 mvn deploy

 mvn validate

 mvn dependency:tree
Shubhankar Pawar
+ 91-8828360046

You might also like