Java + Spring Boot track

Lesson 21 · Part 5: Modern Java

Maven and Gradle

So far, we have treated one or two .java files directly as javac / java. In practice, build tools automate dependencies, compilation, testing, and deployment. The two…

Part 5: Modern Java3 min readFree lesson

So far, we have treated one or two .java files directly as javac / java. In practice, build tools automate dependencies, compilation, testing, and deployment. The two major build tools in the Java camp are Maven and Gradle.

Learning Objectives

  • Know Maven’s pom.xml structure
  • Know Gradle’s build.gradle structure
  • Add external dependencies
  • Know the standard directory structure (src/main/java, src/test/java)
  • Automate compilation, execution, and testing with the mvn / gradle command.

Key concepts

1) Standard directory structure

project/
├── pom.xml (or build.gradle)
└── src/
    ├── main/
│ ├── java/ # operating code
│ └── resources/ # configuration file
    └── test/
├── java/ # test code
        └── resources/

2) Maven pom.xml

<project>
    <groupId>com.codingnow</groupId>
    <artifactId>lecture-modern21</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
    </properties>

    <dependencies>
        <!--Example: SLF4J Logging API-->
    </dependencies>
</project>

Key commands:

mvn compile      #compilation
mvn package      #create jar
mvn test         #test
mvn clean        #Delete build results
mvn exec:java -Dexec.mainClass="..."   #run

3) Gradle build.gradle

plugins {
    id 'application'
    id 'java'
}

group = 'com.codingnow'
version = '1.0.0'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

application {
    mainClass = 'com.codingnow.lecture.modern21.App'
}

Key commands:

gradle build      #build
gradle run        #Runs when an application plugin is present
gradle test       #test
gradle clean

4) Maven vs Gradle comparison at a glance

Item Maven Gradle
Settings XML Groovy/Kotlin DSL
learning curve low Slightly steep
speed standard Fast with progressive build cache
Flexibility standard flow Very flexible
Where to use Traditional companies Android, Modern Project

From Part 22 of this tutorial onwards, Maven will be used as the default.

See as example

Example 1 — maven-demo/pom.xml: Minimal Maven project

Look at maven-demo/pom.xml and src/main/java/com/codingnow/lecture/modern21/App.java in this folder.

cd 05_Modern_Java/21_Maven_Gradle/maven-demo
mvn -q compile
mvn -q exec:java -Dexec.mainClass="com.codingnow.lecture.modern21.App"

Execution result

[Maven] Hello, Java 21!

Memo: mvn exec:java automatically downloads and runs if exec-maven-plugin is not present.

Example 2 — maven-demo/src/.../App.java: Operational code

package com.codingnow.lecture.modern21;

public class App {
    public static void main(String[] args) {
        System.out.println("[Maven] Hello, Java " + Runtime.version().feature() + "!");
    }
}

Note: The source of the Maven project is located under src/main/java/package-path/part.

Example 3 — gradle-demo/build.gradle: Minimal Gradle project

cd 05_Modern_Java/21_Maven_Gradle/gradle-demo
gradle -q run

Execution result

[Gradle] Hello, Java 21!

Note: The application plugin makes gradle run available.

Example 4 — gradle-demo/src/.../App.java: Operational code

package com.codingnow.lecture.modern21;

public class App {
    public static void main(String[] args) {
        System.out.println("[Gradle] Hello, Java " + Runtime.version().feature() + "!");
    }
}

Common Mistakes

  1. The directory structure is not aligned → the build tool cannot find the code.
  2. Using a build tool version that does not support JDK 21 → Compilation error
  3. Only mvn compile and do not know the main class execution command
  4. Confusion between Gradle task names and options (e.g. clean build order)
  5. First download of dependencies fails in an environment without internet

Cleanup

  • In practice, build tools are standard
  • src/main/java + src/test/java is the standard layout
  • Maven is simple and traditional, Gradle is flexible and fast.
  • After part 22 of this tutorial, the focus will be on Maven.

Try it yourself

cd 05_Modern_Java/21_Maven_Gradle/maven-demo
mvn -q compile exec:java -Dexec.mainClass="com.codingnow.lecture.modern21.App"

cd ../gradle-demo
gradle -q run

Example files

The runnable files used in this lesson. Copy them into your own project and experiment.

gradle-demo/build.gradleText
plugins {
    id 'application'
    id 'java'
}

group = 'com.codingnow'
version = '1.0.0'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

repositories {
    mavenCentral()
}

application {
    mainClass = 'com.codingnow.lecture.modern21.App'
}
gradle-demo/settings.gradleText
rootProject.name = 'lecture-modern21-gradle'
gradle-demo/src/main/java/com/codingnow/lecture/modern21/App.javaJava
package com.codingnow.lecture.modern21;

public class App {
    public static void main(String[] args) {
        System.out.println("[Gradle] Hello, Java " + Runtime.version().feature() + "!");
    }
}
maven-demo/pom.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codingnow</groupId>
    <artifactId>lecture-modern21-maven</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
</project>
maven-demo/src/main/java/com/codingnow/lecture/modern21/App.javaJava
package com.codingnow.lecture.modern21;

public class App {
    public static void main(String[] args) {
        System.out.println("[Maven] Hello, Java " + Runtime.version().feature() + "!");
    }
}

Assignment

Problem 1 — Adding external library dependencies

  • Reference solution: the Maven project shown below
  • Core concept: Maven dependency declaration

Requirements

  • Use StringUtils.reverse(...) from Apache Commons Lang3 in the operational code.
  • Add commons-lang3 dependency to pom.xml.
  • In the main class say "Hello, Java!" Reverse it and print it out.

Expected output

Original: Hello, Java!
Flip: !avaJ ,olleH

Hint

  • Dependency coordinates: org.apache.commons:commons-lang3:3.17.0
  • Run: mvn -q compile exec:java -Dexec.mainClass="com.codingnow.lecture.modern21.HwApp"

Check the correct answer

After solving it yourself, compare it with the Maven project in the reference solution below.

Show the reference solution

Try the assignment yourself first — comparing your attempt with a working solution is where most of the learning happens.

maven-demo/pom.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codingnow</groupId>
    <artifactId>lecture-modern21-hw</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.17.0</version>
        </dependency>
    </dependencies>
</project>
maven-demo/src/main/java/com/codingnow/lecture/modern21/HwApp.javaJava
package com.codingnow.lecture.modern21;

import org.apache.commons.lang3.StringUtils;

/** Demo using StringUtils.reverse from Commons Lang3.*/
public class HwApp {
    public static void main(String[] args) {
        String s = "Hello, Java!";
        System.out.println("Original:" + s);
        System.out.println("Flip:" + StringUtils.reverse(s));
    }
}

Learn this lesson with a mentor

Work through the Java + Spring Boot track on your own, or join a guided cohort at our Pitampura campus and online.

Call NowEnroll Now