← Back to Java series
πŸ”§
Modern Java
Modern Java Β· Prerequisite: previous lecture

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**.

JavamodernMavenGradlebuild tool
Duration
⏱ ~1.5-2 hours
Level
πŸ“Š Intermediate-Advanced
Prerequisite
🎯 Previous lecture or equivalent knowledge
OUTCOME
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

text
project/
β”œβ”€β”€ pom.xml         (or build.gradle)
└── src/
    β”œβ”€β”€ main/
    β”‚   β”œβ”€β”€ java/        # production code
    β”‚   └── resources/   # config files
    └── test/
        β”œβ”€β”€ java/        # test code
        └── resources/

2) Maven `pom.xml`

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`

kotlin
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

MavenGradleMeaning
`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

xml
<!-- 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>
java
// src/main/java/Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Maven!");
    }
}
bash
mvn compile
mvn exec:java -Dexec.mainClass=Hello

Example 2 β€” Minimal Gradle project

kotlin
// build.gradle.kts
plugins { java; application }
application { mainClass.set("Hello") }
java { toolchain { languageVersion.set(JavaLanguageVersion.of(21)) } }
bash
gradle build
gradle run

Example 3 β€” Adding a logger

xml
<!-- pom.xml dependency -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>2.0.13</version>
</dependency>
java
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

  1. Mixing Maven and Gradle in the same project
  2. Forgetting `<scope>test</scope>` and shipping test libraries to production
  3. Committing the build cache (`target/` or `build/`) to git
  4. Wrong JDK version in `maven.compiler.source` vs your actual JDK
  5. 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

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

bash
mvn -v
mvn compile
mvn exec:java -Dexec.mainClass=Hello

Next Lecture

[22_JUnit](../22_JUnit/) β€” JUnit 5 unit testing.

Example code / lecture materials

All lecture materials and example code are openly available on GitHub.

View on GitHub β†—