Maven
-Ashmeet Kaur

Maven

Here , In this article you will see step by step setup of maven and why we need maven in our java project.

What is a maven ?

Apache Maven is the most used build management tool for java project. Its based on POM(Page Object Model).Without Maven , Everytime we have to include jars in our project but with maven we just need to add dependency in our pom.xml file present in our project

How to download Maven in Windows ?

  1. Download maven and Extract it.
  2. Add Java_HOME and Maven_HOME in environment variable.
  3. Add Maven path in enviroment variable .
  4. Verify Maven

This is pom.xml file present in our maven project.

<?xml version="1.0" encoding="UTF-8"?
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Maven_project2</artifactId>
    <version>1.0-SNAPSHOT</version>


    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.31</version>
    </dependency>
    </dependencies>


    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>>        

What is a maven Repository?

A maven repository is a directory of packaged JAR file with pom.xml. There are three types of maven repositories:

  1. Central Repository - You don't need authentication to download from the central repository.
  2. Remote Repository - Authentication and Authorization Layer. A private cloud that is accesible to few people.
  3. Local Repository - It is there in your local machine .for example .m2

How maven resolves dependencies ?

It scans all dependencies in pom.xml file. First searches in the local repository. If found, its quits. If in case he is not able to find then it scans in the central repository. If not found in central, it throws error.

Lifecycle of Maven

Lifecycle is the different stage of project Building .

  1. Clean(mvn clean) - Delete the old bytecode.it deletes the target folder.(Target folder holds the class file).
  2. Validate(mvn validate) - Its just validates whether both the  test and the main folders are present.
  3. Compile(mvn compile) - If we have anycompilation error , it will throw that error. It does not compile any test folder.
  4. Test(mvn test) - Checks the manin file also. If we make any changes in the test file.then it changes the test class in target folder.

5. Package(mvn package) - Downloads dependency from the central repository if not found in the local repository. Its adds the  jar in the target folder but that jar is not present in the local. 

6. Verify(mvn verify) - Its verifies whether the jar is present or not.

7. Install(mvn install) - It moves the jar and the pom  that is present in the target folder to local repository that is .m2 so that we can add one project as a dependency and use it other project.

You can find the github link for this project below where maven has been used for more clarity

https://github.com/ash4491/DatabaseConnectivitywithMaven

To view or add a comment, sign in

Others also viewed

Explore content categories