DEV Community

Cover image for What is Maven?And Pom.xml?
Arul .A
Arul .A

Posted on

What is Maven?And Pom.xml?

What is Maven in Spring Boot?

  • Maven is a build automation and project management tool used in Java projects. In Spring Boot, Maven handles:

  • Downloading dependencies (libraries/jars) automatically

  • Compiling your Java code

  • Running tests

  • Packaging your app into a .jar or .war file

  • Deploying your application

What Maven Actually Does

Maven mainly handles 3 things:

  1. Dependency Management

It downloads required libraries automatically.

Example:
You want to use MySQL in Spring Boot.

Without Maven:

  • search MySQL connector online
  • download jar
  • add to project manually

With Maven:

  • just add this in pom.xml
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.3.0</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Maven downloads it automatically from the Maven repository.

  1. Build Automation

Maven can:

  • compile code
  • run tests
  • package project
  • create .jar or .war

Instead of: javac Main.java

You simply run: mvn compile

To create a JAR: mvn package

  1. Standard Project Structure

Maven forces a clean structure.

Example:

project-name/
│
├── src/
│   ├── main/
│   │   ├── java/
│   │   └── resources/
│   │
│   └── test/
│       └── java/
│
├── pom.xml
Enter fullscreen mode Exit fullscreen mode
  1. What is pom.xml ?

This is the heart of Maven.

POM = Project Object Model

It contains:

  • project name
  • dependencies
  • plugins
  • Java version
  • build settings

Example:

<project xmlns="http://maven.apache.org/POM/4.0.0">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>1.0</version>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>3.2.5</version>
        </dependency>

    </dependencies>

</project>
Enter fullscreen mode Exit fullscreen mode

Important Maven Commands :

Command Purpose
mvn compile Compiles code
mvn test Runs tests
mvn package Creates JAR/WAR
mvn clean Deletes old build files
mvn install Installs into local repository

Top comments (0)