Lecture 3 — Master Apache Maven
POM, dependencies, repositories, and the build lifecycle
Video: Master Apache Maven | POM, Dependencies & Lifecycle | Full Course #3
Playlist index: 3
Goal
After this lecture you can:
- Read a
pom.xmland explaingroupId,artifactId,version. - Add a dependency and know where Maven downloads it from.
- Run key lifecycle phases (
clean,compile,package) and know whattarget/is.
Maven is how almost every Spring Boot project is built. Tracking Boot code without Maven literacy is painful.
Concepts
Why Maven exists
Hand-managing JARs does not scale:
- Wrong versions
- Missing transitive libraries
- “Works on my machine” classpath chaos
Maven is a build + dependency + project tool. It downloads libraries, compiles, tests, and packages your app using a standard layout.
Standard project layout
my-app/
pom.xml
src/
main/
java/ ← application code
resources/ ← application.properties, etc.
test/
java/ ← tests
target/ ← build output (generated; do not edit)
Coordinates (GAV)
Every artifact is identified by:
| Field | Example | Meaning |
|---|---|---|
groupId |
com.example |
Organization / namespace |
artifactId |
demo |
Project name |
version |
0.0.1-SNAPSHOT |
Release version |
Together: com.example:demo:0.0.1-SNAPSHOT.
POM (Project Object Model)
pom.xml is the project’s declaration: coordinates, packaging (jar/war/pom), dependencies, plugins, Java version, parent BOM (Spring Boot parent often manages versions for you).
Repositories
| Repo | Role |
|---|---|
| Maven Central | Public default remote repository |
Local ~/.m2/repository |
Cache of downloaded artifacts on your machine |
First build downloads; later builds reuse the local cache.
Super POM vs effective POM
- Super POM: Maven’s built-in defaults.
- Your POM: What you write.
- Effective POM: Merged result (your POM + parents + Super POM).
Inspect with:mvn help:effective-pom
Build lifecycle (default)
Important phases (in order):
validate → compile → test → package → verify → install → deploy
| Phase | What it roughly does |
|---|---|
compile |
Compile main sources |
test |
Run unit tests |
package |
Build the JAR/WAR in target/ |
install |
Put the artifact into your local .m2 |
clean |
Delete target/ (separate clean lifecycle) |
Running a phase runs all previous phases in that lifecycle too (package implies compile + test, unless you skip tests).
Practice — hands-on in your editor / terminal
Use either:
- The Spring Boot project from lecture 02, or
- A fresh Maven project from Initializr (Maven + Java, even without Web)
Step 1 — open and read pom.xml
Find and write down (in your notes):
groupIdartifactIdversionparent(if present — Boot projects usually inheritspring-boot-starter-parent)- One
<dependency>block (e.g.spring-boot-starter-web)
Step 2 — confirm Maven works
In the project root (folder that contains pom.xml):
mvn -vIf the IDE bundles Maven, you can use the IDE’s Maven tool window instead of the CLI.
Step 3 — compile
mvn compileConfirm target/classes appears with .class files.
Step 4 — package
mvn packageFind the JAR under target/ (name includes artifactId + version).
For Boot apps this is often an executable “fat” JAR.
Step 5 — clean
mvn cleantarget/ should be removed (or emptied). Run package again to regenerate it.
Step 6 — see the effective POM (optional but useful)
mvn help:effective-pom > effective-pom.xmlSkim it: notice how much configuration came from the parent, not your short pom.xml.
Step 7 — add a dependency deliberately
- Pick a small library on Maven Central (or Maven Repository), e.g. a JSON helper you are curious about.
- Copy the
<dependency>…</dependency>snippet into yourpom.xml<dependencies>section. - Reload Maven in the IDE / run
mvn compile. - Confirm the JAR appears under External Libraries (IDE) or in
~/.m2/repository/....
Then remove the dependency if you do not need it — the point is learning the workflow.
How this connects to Spring Boot
When Initializr added Spring Web, it added a dependency like spring-boot-starter-web to the POM. That starter pulls Tomcat, Spring MVC, Jackson, etc. Maven resolved that graph; Boot did not magically invent JARs on disk.
Exercises
- Without looking, write the meaning of
groupId,artifactId,version. - Explain the difference between
mvn compileandmvn package. - Where does Maven store downloaded JARs on your machine?
- Why should you usually not commit the entire
target/folder to git?
Self-check
- What does
SNAPSHOTin a version often signal? - True/False:
mvn packagealways uploads your JAR to Maven Central. - Name three default-lifecycle phases in order.
Answers: self-check-answers