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, test, and package your code. POM stands for Project Object Model. A POM is an XML file that describes your project: its name, its Java version, and the libraries it needs.
In this chapter you will create a small Maven project and read its POM line by line.
Step 1: Make the folders
Maven expects the standard layout from the earlier chapter. Create a folder called hello with this structure:
hello/pom.xmlhello/src/main/java/com/example/App.javahello/src/test/java/com/example/AppTest.java
Step 2: Add the code
Put this class in 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 + "!";
}
}Put this test in AppTest.java. A test is code that checks other code. This one uses JUnit 5, 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"));
}
}Step 3: Write the pom.xml
Save this in hello/pom.xml:
<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>Reading the POM
| Part | What it means |
|---|---|
| `modelVersion` | The POM format version. It is always `4.0.0` for now. |
| `groupId`, `artifactId`, `version` | The project's coordinates, its unique name. |
| `packaging` | The kind of file to build. `jar` makes a JAR file. |
| `properties` | Settings. Here: compile for Java 17 and read files as UTF-8. |
| `dependencies` | Libraries the project uses. JUnit is only needed for tests. |
| `build` / `plugins` | Extra tools. Surefire is the plugin that runs tests. |
A dependency is a library your code needs. The <scope>test</scope> line tells Maven to use JUnit only when it compiles and runs tests. JUnit is left out of your finished app.
Step 4: Build it
From the hello folder, run:
mvn package
java -cp target/hello-1.0-SNAPSHOT.jar com.example.AppMaven downloads JUnit, compiles both classes, runs the test, and builds target/hello-1.0-SNAPSHOT.jar. The second command prints Hello, World!.
Exercise
You change <artifactId>hello</artifactId> to <artifactId>greeter</artifactId> and run mvn package. What is the name of the JAR file in target?
Show answer
greeter-1.0-SNAPSHOT.jar. Maven names the JAR from the artifactId and the version.
Exercise
Why does the JUnit dependency have <scope>test</scope>?
Show answer
JUnit is only needed to compile and run the tests. The test scope keeps it off the main classpath, so it is not part of the finished application.