Skip to content

Expert · JDBC, Maven, Gradle and JUnit · topic 48 of 60 · 25 min

Maven: building and dependencies

Explanation

Maven is a build tool: it downloads libraries (dependencies) from Maven Central, compiles your code, runs tests and packages a JAR, all from one pom.xml file and a standard folder layout. Commands follow a lifecycle: mvn compile, test, package, install. The Maven Wrapper (./mvnw) lets anyone build without installing Maven.

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

output
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

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>
Coordinates groupId:artifactId:version identify every library.

The lifecycle

CommandDoes (and runs all earlier phases)
mvn compileCompiles src/main/java into target/classes
mvn testCompiles and runs the tests in src/test/java
mvn packageBuilds the JAR/WAR in target/
mvn verifyRuns extra checks (integration tests, quality plugins)
mvn installCopies the JAR into your local repository (~/.m2) for other local projects
mvn cleanDeletes 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.

Careful: Version conflicts are the most common Maven headache: two libraries need different versions of the same dependency, and Maven picks one ("nearest wins"), which may cause NoSuchMethodError at run time. Use mvn dependency:tree to see what was chosen, and manage versions in one place (a BOM or dependencyManagement). Also never commit target/ to Git.
Think of it like this: pom.xml is like a recipe card that lists the ingredients with exact brands and pack sizes (dependencies and versions) and the cooking steps (the lifecycle). Any cook (developer or CI server) following the card gets the same dish, and the shop (Maven Central) delivers missing ingredients automatically.
Tip: Pin versions explicitly and update them deliberately. Tools like Dependabot or Renovate open pull requests when new versions or security fixes are released.

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

Maven

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.

bash
./mvnw -B clean verify          # -B: batch mode, no interactive output
ls target/*.jar                  # payments-service-1.4.0.jar

Practice (0/3 done)

Task 1 · Question

Which command compiles the code, runs the tests and builds the JAR?

Task 2 · Question

Where should JUnit test classes go in a Maven project?

Task 3 · Practice

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

Question 1

1.What does <scope>test</scope> mean for a dependency?

Question 2

2.Where does Maven cache downloaded JARs?

Question 3

3.What does SNAPSHOT in a version mean?

Question 4

4.Which command shows why a particular library version was chosen?

Read more (free)

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

Answer: Gradle

Android builds are Gradle-based.

From Gradle in brief

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.

From Packages, visibility and project layout

Which command runs the tests in a Maven project?Related

Answer: mvn test

The test phase runs the Surefire plugin.

From Unit testing with JUnit

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.

From Setting up your tools

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

  1. 2 more topics in “JDBC, Maven, Gradle and JUnit” before its exam: Gradle in brief, Unit testing with JUnit.
  2. Module 10 exam: JDBC, Maven, Gradle and JUnit · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Spring Boot basics. 60 topics left in the course before the final exam.

← Previous: SQL injection and safe queries