Stop 11 · Git, Build Tools & TestingTopics · 4/10

Maven

Part of the Java Roadmap

In short

Real Java projects use dozens of libraries and must build the same way on every laptop and server. Maven handles downloading those libraries, compiling, testing and packaging with one command.

Maven manages dependencies and builds Java projects using a pom.xml file and a standard folder layout (src/main/java, src/test/java). It is very common in enterprise Java and Spring projects.

The jaadu in a chef's apron reads a recipe card pinned to the wall while neatly lined-up ingredient jars wait on the counter.

Think of it like this

The restaurant recipe card

A restaurant kitchen doesn't rely on the chef's memory. A recipe card lists the ingredients and quantities, and every cook follows the same steps in the same order. pom.xml is that card: it lists your ingredients (dependencies) and Maven follows a fixed set of steps (the lifecycle) to cook the dish.

Watch it happen

What mvn package runs

validate
compile
test
package

1/4Maven checks that pom.xml is valid and downloads missing dependencies into ~/.m2/repository.

The big ideas

What you need to know

Standard layout

src/main/java for code, src/main/resources for config, src/test/java for tests, and output goes to target/.

Coordinates

Every library is identified by groupId, artifactId and version, like org.junit.jupiter : junit-jupiter : 5.11.3.

Scopes

compile (default) is needed everywhere, test only for tests, provided is supplied by the server at runtime.

Daily commands

mvn clean package, mvn test, mvn dependency:tree. Use the ./mvnw wrapper so everyone runs the same Maven version.

See it in code

Walk through the code

xml
1<project xmlns="http://maven.apache.org/POM/4.0.0">2  <modelVersion>4.0.0</modelVersion>3 4  <groupId>com.example</groupId>5  <artifactId>bank-app</artifactId>6  <version>1.0.0-SNAPSHOT</version>7 8  <properties>9    <maven.compiler.release>21</maven.compiler.release>10    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>11  </properties>12 13  <dependencies>14    <dependency>15      <groupId>org.junit.jupiter</groupId>16      <artifactId>junit-jupiter</artifactId>17      <version>5.11.3</version>18      <scope>test</scope>19    </dependency>20  </dependencies>21 22  <build>23    <plugins>24      <plugin>25        <artifactId>maven-surefire-plugin</artifactId>26        <version>3.5.2</version>27      </plugin>28    </plugins>29  </build>30</project>

1/4Your project's own coordinates. SNAPSHOT means a work-in-progress version.

A minimal pom.xml for a Java 21 project with JUnit 5. Run mvn test to compile and run the tests.

Save yourself the pain

Common mistakes

Oops

Downloading jar files by hand and putting them in a lib folder.

Do this instead

Declare the dependency in pom.xml. Maven fetches it and its own dependencies from Maven Central.

Oops

Committing the target/ folder to Git.

Do this instead

target/ is generated on every build. Add it to .gitignore.

Oops

Leaving JUnit with the default compile scope.

Do this instead

Use <scope>test</scope> so testing libraries stay out of production code.

Your mission

Your first Maven project

~30 min

Quick check

You run mvn package. Which phases run?

Go deeper

  1. 1Create a Maven project and add a dependency from Maven Central.
  2. 2Run mvn clean package and find the generated JAR.
  3. 3Learn the main lifecycle phases: compile, test, package and install.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge