Detailed Blog's On Maven

Detailed Blog's On Maven

What is Maven?

Maven is a powerful project management tool.

Based on the concept of a project object model (POM).

It is used for project 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.

it was written in java.

Maven is an open-source project management build tool developed by Apache.

What are dependencies?

it refers to the java libraries that are needed for that project.

What are Repositories?

Repositories are like storage where we can store our code.

What are Build tools?

A build tool takes care of everything for building a process.

example: JAVA - Ant,Maven, Gradle

Understanding the problem without Maven

There are many problems that we face during the project development. They are discussed below:

1) Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need to add set of jar files in each project. It must include all the dependencies of jars also.

2) Creating the right project structure: We must create the right project structure in servlet, struts etc, otherwise, it will not be executed.

3) Building and Deploying the project: We must have to build and deploy the project so that it may work.

example:

Let’s consider we have to build a web application. We might have several dependencies such as web mvc, junits, etc. We need to manage all the dependencies, build WAR files, deploy, etc. All these steps take a lot of time.

Let us consider an example:
We have two files: App.java and AppTest.java along with 2 jar files for writing JUnits.

We first need to compile App.java -> App.class

Then we need to compile AppTest.java → AppTest.class

Then we need to run the test cases and check if no tests are failing.

Then combine all classes to the jar file

As you can see, the steps are quite tedious and difficult to follow along, and if you have a huge project with a lot of dependencies to manage it becomes almost impossible. Developers used ANT before to solve and automate the above process, but for this, we had to write long scripts. Maven came in as a solution for this.

Why use maven?

  1. It is a complete project management tool.

  2. It makes the build process very easy (No need to write long scripts).

  3. It has a remote maven repo which has all the dependencies in one place.

  4. It is easy to migrate for new features of Maven

  5. Apache Maven helps to manage

    • Builds

    • Documentation

    • Reports

    • Releases

    • Distribution

### **<mark>What is Maven pom.xml file</mark>**

**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.

**Elements of POM:**

1. project: root element of the XML file

2. modelVersion: should be set to 4.0.0

3. groupId: It specifies the id of the project’s group

4. artifactId: id of the project

5. version: Version of the artifact within the group

    We have several other elements which include: packaging, dependencies, dependencyManagement, repositories, scm, modules, build, etc.

    All the dependencies are downloaded from a remote repository into a local maven repository. This is why, while running any new project, it takes time for the project to load, as it is downloading the dependencies from remote to local.

Maven build lifecycle

Maven build lifecycle is a set of phases that is executed when a maven build is run. There are three build lifecycles: default, clean, and site. The default lifecycle comprises of the following phases:

  • validate — validate the project is correct and all necessary information is available

  • compile — compile the source code of the project

  • test — test the compiled source code using a suitable unit testing framework. These tests should not require the code to be packaged or deployed

  • package — take the compiled code and package it in its distributable format, such as a JAR.

  • verify — run any checks on results of integration tests to ensure quality criteria are met

  • install — install the package into the local repository, for use as a dependency in other projects locally

  • deploy — done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.

These lifecycle phases are executed sequentially to complete the default lifecycle.

The architecture of Mvaen:

Everything You Want to Know About Maven – An Automation Tool for Java

All the dependencies are downloaded from a remote repository into a local maven repository. This is why, while running any new project, it takes time for the project to load, as it is downloading the dependencies from remote to local.

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:

  1. Local Repository:

    Maven local repository is located in your local system. It is created by the maven when you run any maven command.

  2. Central Repository:

    Maven central repository is located on the web. It has been created by the apache maven community itself.

  3. 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 searches for the dependencies in the following order:

    Local repository then Central repository then Remote repository.

    maven repositories

Difference between Ant and Maven

Ant and Maven both are build tools provided by Apache. The main purpose of these technologies is to ease the build process of a project.

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

There is no life cycle in Ant.

There is life cycle in Maven.

It is a tool box.

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 maven works?

What Is Maven - Maven Tutorial For Beginners

Build Life Cycles, Phases and Goals: A build life cycle consists of a sequence of build phases, and each build phase consists of a sequence of goals.

Build Profiles: Build profiles a set of configuration values which allows you to build your project using different configurations. For example, you may need to build your project for your local computer, for development and test. To enable different builds you can add different build profiles to your POM files using its profiles elements and are triggered in the variety of ways.

Build Plugins: Build plugins are used to perform specific goal. you can add a plugin to the POM file. Maven has some standard plugins you can use, and you can also implement your own in Java.

When should someone use Maven?

One can use the Maven Build Tool in the following condition:

    1. When there are a lot of dependencies for the project. Then it is easy to handle those dependencies using maven.

      1. When dependency version update frequently. Then one has to only update version ID in pom file to update dependencies.

      2. Continuous builds, integration, and testing can be easily handled by using maven.

      3. 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.

Thank you for reading this blog. Hope it helps.

— Safia Khatoon