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:
- 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>
Maven downloads it automatically from the Maven repository.
- 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
- Standard Project Structure
Maven forces a clean structure.
Example:
project-name/
│
├── src/
│ ├── main/
│ │ ├── java/
│ │ └── resources/
│ │
│ └── test/
│ └── java/
│
├── pom.xml
- 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>
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)