Concept Explanation
Building a Java project is not just about writing code; it's about Cooking.
- The Recipe (pom.xml): This is a file that tells Maven everything about your project. What name it has, what ingredients (dependencies) it needs, and how to cook (build) it.
- The Grocery Store (Maven Central): If your project needs a "Jar of Spring Framework," Maven doesn't make you go find it yourself. It automatically goes to the "Grocery Store" (an online repository), downloads it, and adds it to your kitchen.
The Maven Lifecycle
Maven has a standard set of steps it follows:
- Compile: Turns your Java code into bytecode.
- Test: Runs all your unit tests.
- Package: Zips everything up into a JAR or WAR file.
- Install: Puts your JAR in your local kitchen so your other projects can use it.
Syntax (The pom.xml)
<dependencies>
<dependency>
<groupId>com.google.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
Practice Exercises
- The Dependency Hunt: Go to Maven Central. Search for the "JUnit" library. Look at the XML snippet you would need to add it to a project.
- Clean and Install: Research what
mvn clean installdoes. Why is it helpful to "clean" before you "install"? - Group vs Artifact: In Maven terminology, what is the difference between a
groupIdand anartifactId?