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.xmlstructure - Know Gradle’s
build.gradlestructure - Add external dependencies
- Know the standard directory structure (
src/main/java,src/test/java) - Automate compilation, execution, and testing with the
mvn/gradlecommand.
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
- The directory structure is not aligned → the build tool cannot find the code.
- Using a build tool version that does not support JDK 21 → Compilation error
- Only
mvn compileand do not know the main class execution command - Confusion between Gradle task names and options (e.g.
clean buildorder) - First download of dependencies fails in an environment without internet
Cleanup
- In practice, build tools are standard
src/main/java+src/test/javais 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