Expert · JDBC, Maven, Gradle and JUnit · topic 48 of 60 · 25 min
Maven: building and dependencies
Explanation
Why a build tool?
A real project uses dozens of libraries (a web framework, a database driver, JSON, logging, testing), each with its own dependencies. Downloading JARs by hand and writing long javac commands does not scale. Maven describes the project in one file, fetches the right versions automatically and builds it the same way on every machine and CI server.
Standard folder layout
shop-api/
├── pom.xml
├── src/main/java/com/shop/App.java ← application code
├── src/main/resources/application.properties
├── src/test/java/com/shop/AppTest.java ← tests
└── target/ ← build output (do not commit)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId> <!-- your organisation -->
<artifactId>shop-api</artifactId> <!-- the project name -->
<version>1.0.0-SNAPSHOT</version> <!-- SNAPSHOT = still in development -->
<properties>
<maven.compiler.release>21</maven.compiler.release>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.4</version> <!-- example version: check Maven Central for the latest -->
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.3</version> <!-- example version -->
<scope>test</scope> <!-- only for tests, not shipped -->
</dependency>
</dependencies>
</project>The lifecycle
| Command | Does (and runs all earlier phases) |
|---|---|
| mvn compile | Compiles src/main/java into target/classes |
| mvn test | Compiles and runs the tests in src/test/java |
| mvn package | Builds the JAR/WAR in target/ |
| mvn verify | Runs extra checks (integration tests, quality plugins) |
| mvn install | Copies the JAR into your local repository (~/.m2) for other local projects |
| mvn clean | Deletes target/ (often: mvn clean package) |
Maven downloads dependencies from Maven Central (search.maven.org) into a local cache, ~/.m2/repository, so each version is downloaded only once. Transitive dependencies (the libraries your libraries need) are fetched automatically; mvn dependency:tree shows the full tree.
The Maven Wrapper
Projects usually include mvnw / mvnw.cmd and a .mvn folder. Running ./mvnw package (or mvnw.cmd package on Windows) downloads the exact Maven version the project expects. Spring Initializr adds the wrapper automatically.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Maven: building and dependencies · 1/6
In this lesson you will build Java projects with Maven.
Prefer a video by a teacher? Find free videos on “maven tutorial pom.xml dependencies lifecycle mvn package” on YouTube ↗
In real life: A CI pipeline for a payments service
Every push to a fintech company's payments service triggers a CI job that runs ./mvnw -B verify: it compiles, runs 900 unit tests and integration tests, checks code style, and builds the JAR. If any step fails, the change cannot be merged.
./mvnw -B clean verify # -B: batch mode, no interactive output
ls target/*.jar # payments-service-1.4.0.jarPractice (0/3 done)
Which command compiles the code, runs the tests and builds the JAR?
Where should JUnit test classes go in a Maven project?
Create a Maven project (with your IDE or "mvn archetype:generate"), set Java 21 in the pom, add JUnit Jupiter with scope test, write one class and one test, then run mvn test and mvn package and find the JAR in target/.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Gradle in briefJDBC, Maven, Gradle and JUnit · same module
- JDBC: talking to a databaseJDBC, Maven, Gradle and JUnit · same module
- Packages, visibility and project layoutGo Programming (backend and cloud) · Packages, testing and generics
- Setting up your toolsFull-Stack Development · What full-stack development is
- Installing software with dnf and rpmLinux and Red Hat (RHCSA to RHCE) · Software and networking
- Installing .NET and using the dotnet CLIC# and .NET · Getting started with C# and .NET
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What does <scope>test</scope> mean for a dependency?
Answer: It is only available when compiling and running tests
Test-scoped libraries are not part of the final artifact.
Where does Maven cache downloaded JARs?
Answer: ~/.m2/repository
The local repository.
What does SNAPSHOT in a version mean?
Answer: A version still in development
Release versions drop -SNAPSHOT.
Which command shows why a particular library version was chosen?
Answer: mvn dependency:tree
It prints the resolved dependency tree.
Which build tool do Android projects use?Related
Is the popular "project-layout" repository an official Go standard?Related
Answer: No; the Go team recommends starting simple
It is a community repo, not an official standard.
Which command runs the tests in a Maven project?Related
Which Node.js version should a beginner install?Related
Answer: The version nodejs.org marks as LTS
LTS versions get bug and security fixes for a long time. Even-numbered majors become LTS.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “JDBC, Maven, Gradle and JUnit” before its exam: Gradle in brief, Unit testing with JUnit.
- Module 10 exam: JDBC, Maven, Gradle and JUnit · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Spring Boot basics. 60 topics left in the course before the final exam.