Sub topic 5
Your First Maven Project and the pom.xml
Maven is a build tool for Java. It reads one file, called pom.xml, and uses it to compile your code, run your tests, and package everything into a JAR file. A JAR file is a single archive that holds your compiled classes. This chapter shows you how to set up a small Maven project and what each part of its pom.xml means.
You already know the standard folder layout and artifact coordinates from earlier chapters. Here you will put them together in a working project.
What a POM Is
POM stands for Project Object Model. The POM is an XML file named pom.xml that sits in the top folder of your project. XML is a text format that wraps data in tags, like <name>value</name>.
The POM describes your project. It does not list build steps one by one. Instead, it tells Maven facts like these:
- The project's name and version.
- The Java version to compile for.
- The libraries the project needs.
- The plugins Maven should use and their versions.
Maven already knows how to build a normal Java project, so it fills in the steps itself. This idea is called convention over configuration. You follow Maven's usual way of doing things, and you only write down what is different about your project.
Setting Up the Project Folders
Make a folder called hello. Inside it, create this layout:
mkdir -p hello/src/main/java/com/example
mkdir -p hello/src/test/java/com/example
cd helloYour project will end up looking like this:
| Path | What goes there |
|---|---|
| `pom.xml` | The project description Maven reads |
| `src/main/java/com/example/App.java` | Your application code |
| `src/test/java/com/example/AppTest.java` | Your test code |
| `target/` | Output Maven creates; never edit it by hand |
Adding the Java Code
Save this class as src/main/java/com/example/App.java:
package com.example;
public class App {
public static void main(String[] args) {
System.out.println(greeting("World"));
}
static String greeting(String name) {
return "Hello, " + name + "!";
}
}Save this test as src/test/java/com/example/AppTest.java. A test is code that checks your other code works. This one uses JUnit 5, which is a popular testing library.
package com.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void greetsByName() {
assertEquals("Hello, Maven!", App.greeting("Maven"));
}
}Writing the pom.xml
Save this file as pom.xml in the hello folder:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>hello</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.5</version>
</plugin>
</plugins>
</build>
</project>The next sections explain this file one part at a time.
The project element and modelVersion
The <project> tag wraps the whole file. The xmlns and xsi lines at the top tell editors which XML rules the file follows. Your editor can then warn you about typos. Maven itself does not need these lines, but keeping them is good practice.
<modelVersion> gives the version of the POM format. For Maven 3, it is always 4.0.0. Do not change it.
Coordinates and packaging
These four lines name your project:
groupIdis the group or company that owns it, herecom.example.artifactIdis the project's own name, herehello.versionis its version.SNAPSHOTmeans "still in progress, not a final release."packagingis the kind of output to make.jaris the default, so you could leave this line out.
Maven uses these values to name the output file. It joins artifactId and version to get hello-1.0-SNAPSHOT.jar.
Properties
<properties> holds settings as name and value pairs.
maven.compiler.releaseset to17tells the compiler to build for Java 17. Your code then can't use features from newer Java versions. It also won't run on older ones.project.build.sourceEncodingset toUTF-8tells Maven how to read your source files. Without it, Maven uses your computer's default and prints a warning. The same file could then build differently on two machines.
Dependencies
A dependency is a library your project needs. Each one is named by its own coordinates. Here the project needs JUnit 5 version 5.10.2.
The <scope>test</scope> line means JUnit is only used for test code. It is not included with your application code. A later chapter covers scopes in detail.
Build plugins
A plugin is a piece of Maven that does a real job, like compiling or running tests. Maven itself mostly coordinates the plugins. The Surefire plugin runs your tests.
Why write down its version? Maven comes with default plugin versions, and those defaults change between Maven releases. Older defaults of Surefire do not find JUnit 5 tests. Pinning version 3.2.5 makes sure your tests run, whatever Maven version a person has installed.
Building the Project
First check that Maven is installed:
mvn -vThis prints the Maven version and the Java version it uses. Then build the project from the hello folder:
mvn packageThe first run is slow because Maven downloads its plugins and JUnit from the internet. Later runs are much faster. Near the end, you should see output like this:
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] Building jar: .../hello/target/hello-1.0-SNAPSHOT.jar
[INFO] BUILD SUCCESSNow run your program:
java -cp target/hello-1.0-SNAPSHOT.jar com.example.AppIt prints Hello, World!. That one mvn package command did all of this:
- Compiled the code into
target/classes. - Compiled the test into
target/test-classes. - Ran the test.
- Built the JAR.
The next chapter explains how Maven decides which steps to run.
The Hidden Defaults: the Super POM and Effective POM
Your POM is short because every POM quietly inherits from a built-in file called the Super POM. The Super POM sets defaults like these:
- Source code lives in
src/main/java. - Output goes to
target. - Libraries download from Maven Central.
The effective POM is your POM merged with all these defaults. You can print it:
mvn help:effective-pomLook in the output for <sourceDirectory>. It points to src/main/java, even though you never wrote that. If Maven ever behaves in a way you don't expect, the effective POM is the first place to check.
Common Mistakes
| Problem | Likely cause | Fix |
|---|---|---|
| "Non-parseable POM" | Missing or mismatched closing tag | Check each `<tag>` has its `</tag>` |
| "No sources to compile" | Code is not in `src/main/java` | Move files into the standard layout |
| "release version 17 not supported" | Your JDK is older than 17 | Install JDK 17+ or lower the release value |
| Tests run: 0 | Test in the wrong folder or old Surefire | Put tests in `src/test/java` and pin Surefire |
| Package error at compile | Folder path does not match `package` line | Make folders match, e.g. `com/example` |
Exercises
Exercise
You change the version in pom.xml from 1.0-SNAPSHOT to 1.0.0 and run mvn package. What is the name of the new JAR file in target, and why?
Show answer
It is hello-1.0.0.jar. Maven builds the file name from the artifactId (hello) and the version (1.0.0).
Exercise
You delete the line <maven.compiler.release>17</maven.compiler.release>. Where can you find out which Java version Maven will now compile for?
Show answer
Run mvn help:effective-pom and look at the compiler settings. Your POM no longer sets the release value, so Maven falls back on defaults from the Super POM and the compiler plugin.
Exercise
A teammate's build says BUILD SUCCESS but "Tests run: 0", even though AppTest.java exists in src/test/java/com/example. Name one likely cause in the pom.xml.
Show answer
The Surefire plugin version is not pinned, so an older default version is used that does not detect JUnit 5 tests. Pinning maven-surefire-plugin to 3.2.5 or later fixes it.