Lesson 19 of 23
In progress
Maven, Gradle and the build
Let a build tool resolve dependencies, run tests and package a jar, and understand the layout both tools expect.
Two commands can run a single-file program. A real project has dependencies, tests, resources and several source folders, and a build tool keeps all of that reproducible.
What you will learn
- Read a Maven
pom.xmland a Gradle build file. - Use the standard directory layout.
- Know which commands cover build, test and run.
The standard layout
project/
pom.xml
src/main/java/com/example/App.java
src/main/resources/application.properties
src/test/java/com/example/AppTest.java
Both Maven and Gradle assume this shape by default: main code in src/main/java, resources in src/main/resources, tests in src/test/java. Following it means no configuration; departing from it means explaining every deviation to every tool.
src/main/resources is copied onto the classpath, so getClass().getResourceAsStream("/application.properties") finds it. A file next to the source is not on the classpath and will not be found in a packaged jar.
Maven
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>shop</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>21</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.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Coordinates are group, artifact and version. The scope decides when a dependency is on the classpath: test for JUnit, provided for something the runtime supplies, and the default compile for libraries used in production code.
Commands:
mvn compile # compile main sources
mvn test # compile and run tests
mvn package # produce target/shop-1.0.0.jar
mvn exec:java -Dexec.mainClass=com.example.App
Maven is convention-driven and declarative: the lifecycle is fixed, and plugins hook into phases such as compile, test and package.
Gradle
plugins {
id 'java'
id 'application'
}
java {
toolchain { languageVersion = JavaLanguageVersion.of(21) }
}
repositories { mavenCentral() }
dependencies {
testImplementation platform('org.junit:junit-bom:5.11.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
application { mainClass = 'com.example.App' }
test { useJUnitPlatform() }
./gradlew build
./gradlew test
./gradlew run
Gradle uses a script that can contain logic, which makes it more flexible and more capable of becoming a program of its own. The gradlew wrapper pins the Gradle version, so every machine uses the same one; commit it.
Kotlin DSL (build.gradle.kts) is the modern default for new Gradle projects and gets completion in an IDE.
Which to choose
Both work. Maven is simpler to read, extremely stable, and every Java developer can follow a pom.xml. Gradle is faster on incremental builds and more flexible for multi-module projects.
For a new project with no constraints, either is fine. Pick the one the team already knows; a build tool is not where originality pays.
Dependencies and versions
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.2</version>
</dependency>
Add a dependency for a real need: JSON parsing, a test framework, a logging binding. Each one is code you did not write, with its own bugs and updates.
Version conflicts happen when two libraries need different versions of a third. Maven picks the nearest; Gradle picks the highest by default. mvn dependency:tree and gradle dependencies show the graph, which is the first thing to check when a class is missing at runtime.
Do not commit target/ or build/; they are regenerated. Do commit the wrapper scripts and the lock or pom files, because those define the build.
Running without an IDE
mvn -q package
java -jar target/shop-1.0.0.jar
A jar runs with java -jar only if its manifest names the main class. The Maven Shade or Assembly plugin, or Gradle’s shadow plugin, produces a jar that includes dependencies; otherwise the classpath must be supplied:
java -cp "target/shop-1.0.0.jar:libs/*" com.example.App
The separator is ; on Windows and : elsewhere, which is one reason to let the tool build the classpath rather than typing it.
What the build should guarantee
A build is a promise that anyone can clone the repository and produce the same artefact. That requires:
- The same compiler release, set in the build file rather than in an IDE setting.
- Pinned dependency versions with a lockfile where the tool supports one.
- Tests that run as part of the build, not as a separate ritual.
If a build works on one machine and not another, the difference is usually a version set outside the build file or a file that was never committed.
Adding a dependency in the IDE but not the build file. It compiles locally because the IDE’s classpath has the jar, and fails on a clean checkout. Always add dependencies through the build file, then refresh the project.
Write the Maven coordinates and scope for JUnit 5 and for a JSON library used by main code, then say which command runs only the tests.
Show solution
<dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.11.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.2</version> </dependency> </dependencies> JUnit is test scope because it is only needed to compile and run tests. Jackson has the default compile scope because production code calls it. mvn test runs the tests and stops before packaging.
Recap
- The standard layout puts main code, resources and tests in known folders.
- Maven is declarative with a fixed lifecycle; Gradle is scriptable with a committed wrapper.
- Dependencies and compiler versions belong in the build file so a clean checkout builds identically.
Next, JUnit: writing tests the build can run.
Do I need both tools?
What is a BOM?
Progress is saved in this browser only. No account, no email.