These days, While you working on any kind of java project. you might hear about the maven or using the maven. so maven is part of the development and it’s really important to have a clear understanding of maven.

In this session, we will cover all the basic teams, uses of maven, the problems that are solved by maven, and all the end-to-end processes that you need to know about maven.

All that you need to know about the maven tool

  • What is a maven?
  • Uses of Maven?
  • How to create a maven project?
  • What’s inside the pom.xml
  • How to add or remove maven dependencies in pom.xml

What is  Maven?

Maven is a project management or automation tool or we can call it a build tool. that help to manage the application versions, Create jars, dependencies(third party jars), Unit test or automation test reporting, documentation, etc.

What is a Build tool?

Build tool helps to build a java project in order to build an application it performs the following operations.

  • Generating source code.
  • Compile the source code.
  • Create final jars to deploy the application.
  • Install or manage the dependencies on the Local or server repositories.

What is Page Object Model(POM.xml)?

Maven is based on the Page Object Model. when we create a maven project it generates a pom.xml file. This pom.xml file contains all the information about the project and configuration details.

When we run the maven project it first searches for the pom.xml file in the current directory of the project and the project is executed according to the pom.xml configuration.

Uses or Needs of the maven?

  • Project configuration (Manage project version, Project name, used plugins, target JDK, test Documentation)
  • Dependencies management.

Now what I mean by dependencies management is, Let’s understand this by an example.

Before maven If we need to use other java services with our java application like MYSQL connection, Servlet, Logger, Junit. Then we have to download the jar file and add those jar files into the lib folder of the project.

But assume, their project is running on my machine and all the jars are stored in some folder in my machine and everything is working fine. Now If I move the project to another machine and put those jars at any other location same machine.

But the project assumes the jar location as the previous machine. So the code will not work because the jar is not found at the required location.

Now maven comes into the picture and gives a solution to download the required jar/libraries in real time while building the project. and keep the jar at the dynamic location so it will be the same for all machines and servers.

Where I will find maven dependencies?

In the project, check the pom.xml file, you will get the dependency under the <dependencies></dependencies> tag.

Below is an example of the logger dependency

<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.14</version>
</dependency>

To get the dependency you can just visit the official website of maven “https://mvnrepository.com/“, Search for the required dependency and grab the code and add it to your pom.xml. Once you save the pom.xml automatic project will build and it will add the jar to the maven dependency section.

How to create a maven project in eclipse?

Download and install the latest version of eclipse and run it. now go to files you will get the option to create a maven project. or follow the below video that will give you a clear understanding to create a maven project from scratch.

What’s inside the pom.xml

Once you open the pom.xml file you will find the following things let’s understand them one by one.

It’s an XML file and contains the information in tags. The root tag of the pom.xml file is <project></project>

<project></project>

It defines the location of the maven that we using for this application. as it is a root tag all other tags will come under this <project> tag

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

</project>

<modelVersion></modelVersion> defines the version of maven.

<groupId></groupId>  is defined as a unique name of the community or company that going to develop the application.

<artifactId></artifactId> Name of the application

<packaging></packaging> Defines packaging of the project after building it can be jar or war

<version></version> Defines the version of the application.

<dependencies> </dependencies> Contains all the dependencies used in the application.

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0-alpha-1</version>
    <scope>provided</scope>
</dependency>

    
  </dependencies>

How to add or remove maven dependency in pom.xml

Find the required dependency from the official site of maven “https://mvnrepository.com/” and add it between the <dependencies></dependencies> tag and save the pom.xml. Maven automatically builds the application and downloads the added dependency.

To Remove, Delete the dependency and save the file again maven automatically removes the dependency.

In case it does not update automatically just do a manual maven force update.