21. Maven and Gradle
Until now we've compiled `.java` files by hand with `javac` / `java`. Real projects use a **build tool** to automate dependencies, compilation, testing, and packaging. Java's two main build tools are **Maven** and **Gradle**.
What you'll learn
- 1Know Maven's `pom.xml` structure
- 2Know Gradle's `build.gradle` structure
- 3Add an external dependency
- 4Understand the standard directory layout (`src/main/java`, `src/test/java`)
- 5Automate compile / run / test with `mvn` / `gradle`
Overview
Until now we've compiled `.java` files by hand with `javac` / `java`. Real projects use a **build tool** to automate dependencies, compilation, testing, and packaging. Java's two main build tools are **Maven** and **Gradle**.
Core Concepts
1) Standard directory layout
project/
βββ pom.xml (or build.gradle)
βββ src/
βββ main/
β βββ java/ # production code
β βββ resources/ # config files
βββ test/
βββ java/ # test code
βββ resources/2) Maven `pom.xml`
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codingnow</groupId>
<artifactId>hello</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>3) Gradle `build.gradle.kts`
plugins { java }
group = "com.codingnow"
version = "1.0.0"
java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) } }
repositories { mavenCentral() }
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.10.5")
}
tasks.test { useJUnitPlatform() }4) Common commands
| Maven | Gradle | Meaning |
|---|---|---|
| `mvn compile` | `gradle compileJava` | Compile |
| `mvn test` | `gradle test` | Run tests |
| `mvn package` | `gradle build` | Package |
| `mvn dependency:tree` | `gradle dependencies` | Print dep tree |
5) Adding dependencies
Find an artifact on https://central.sonatype.com and copy the `<dependency>` block (Maven) or `implementation(...)` line (Gradle).
Examples
Example 1 β Minimal Maven project
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codingnow</groupId>
<artifactId>hello-maven</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
</project>// src/main/java/Hello.java
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Maven!");
}
}mvn compile
mvn exec:java -Dexec.mainClass=HelloExample 2 β Minimal Gradle project
// build.gradle.kts
plugins { java; application }
application { mainClass.set("Hello") }
java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) } }gradle build
gradle runExample 3 β Adding a logger
<!-- pom.xml dependency -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
</dependency>import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class WithLogger {
private static final Logger LOG = LoggerFactory.getLogger(WithLogger.class);
public static void main(String[] args) {
LOG.info("Hello, slf4j");
}
}Common Mistakes
- Mixing Maven and Gradle in the same project
- Forgetting `<scope>test</scope>` and shipping test libraries to production
- Committing the build cache (`target/` or `build/`) to git
- Wrong JDK version in `maven.compiler.source` vs your actual JDK
- Not pinning dependency versions and getting flaky builds
Summary
- A build tool automates the lifecycle: dependencies β compile β test β package
- Maven is XML-based; Gradle is Kotlin/Groovy DSL-based
- Pick one for your project and stick with it
Practice
# Practice - 21. Maven and Gradle
## Exercise 1 β `mvn` Hello
- File: `pom.xml` + `src/main/java/Hello.java`
- Key concepts: minimal pom, run with mvn
Requirements
- Set up a minimal Maven project that prints `Hello, Maven!`.
## Exercise 2 β Add SLF4J
- File: `pom.xml`
- Key concepts: dependencies
Requirements
- Add `org.slf4j:slf4j-simple` to dependencies and call `LOG.info("hi")`.
## Solutions After trying it yourself, compare with [`answer/`](./answer/).
Solution code (homework/answer/)
answer/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codingnow</groupId>
<artifactId>hello-maven</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.13</version>
</dependency>
</dependencies>
</project>
Try It Yourself
mvn -v
mvn compile
mvn exec:java -Dexec.mainClass=HelloNext Lecture
[22_JUnit](../22_JUnit/) β JUnit 5 unit testing.
All lecture materials and example code are openly available on GitHub.
View on GitHub β